stu*_*ent 0 android android-layout
我试图将一个 TextView 放在一个相对布局中,但由于某种原因,TextView 的位置并不完全在中心,它在中心并稍微向下。当我添加第二个没有 id 的 RelativeLayout 时会出现此问题。
以下是我尝试解决问题但没有成功的两种方法:
第一的:
<RelativeLayout
android:id="@+id/minus_one"
android:layout_width="45dp"
android:layout_height="match_parent"
android:background="@color/lightgrey">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ractangle_visual_feedback_selector"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:textStyle="bold"
android:text="-"/>
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
第二:
<RelativeLayout
android:id="@+id/minus_one"
android:layout_width="45dp"
android:layout_height="match_parent"
android:background="@color/lightgrey">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ractangle_visual_feedback_selector">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:textStyle="bold"
android:text="-"/>
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
换根Relative Layout
android:layout_width to "match_parent"
。不需要android:gravity="center"
从父项中删除Relative Layout
。现在添加android:layout_centerInParent="true"
到您的Text View
.
试试这个 xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/minus_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Testing"
android:textSize="22dp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)