在 android:layout_centerVertical="true" 方面需要帮助
Need assistance with android:layout_centerVertical="true"
我是 android 上的编程新手,所以我很难理解相对布局的工作原理。我已经记下了大部分内容,但是 android:layout_centerVertical="true" 只是在我尝试在我的代码中使用它时无法正常工作。我应该按从上到下的顺序放置 3 个文本视图,所以它显示 "Happy Birthday To You" 但我无法将 "Birthday" 文本视图放在左中。当我使用 android:layout_centerInParent="true" 时,它在中间居中,我无法向左或向右移动它。有人可以看看我的代码并告诉我我做错了什么吗?谢谢。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Happy" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Birthday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="To You" />
你可以试试android:gravity="center"
Place the object in the center of its container in both the vertical
and horizontal axis, not changing its size.
android:layout_width="match_parent"
android:gravity="left|center" // Text will Center & left align
仅供参考。
您正在使用 RelativeLayout 。对于您的 TEXTVIEW Vertical 问题,您应该使用 android:layout_below.
Positions the top edge of this view below the given anchor view ID.
Accommodates top margin of this view and bottom margin of anchor view.
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center" // settext gravity as per your requirement
android:text="Happy"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:gravity="center"// settext gravity as per your requirement
android:text="Birthday"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center" // settext gravity as per your requirement
android:text="To You"
android:textAppearance="?android:textAppearanceLarge" />
</RelativeLayout>
使用layout_centerVertical="true"
在垂直方向居中视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Happy"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Birthday"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="To You"
android:textAppearance="?android:textAppearanceLarge" />
</RelativeLayout>
只需在父布局中使用android:gravity="center"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Happy" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Birthday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="To You" />
</RelativeLayout>
您的文件现在生成的是:
我猜你想要的是这个:
所以只需在您的根布局中添加以下行,这会将其所有子视图在其中心水平对齐
android:gravity="center_horizontal"
我是 android 上的编程新手,所以我很难理解相对布局的工作原理。我已经记下了大部分内容,但是 android:layout_centerVertical="true" 只是在我尝试在我的代码中使用它时无法正常工作。我应该按从上到下的顺序放置 3 个文本视图,所以它显示 "Happy Birthday To You" 但我无法将 "Birthday" 文本视图放在左中。当我使用 android:layout_centerInParent="true" 时,它在中间居中,我无法向左或向右移动它。有人可以看看我的代码并告诉我我做错了什么吗?谢谢。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Happy" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Birthday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="To You" />
你可以试试android:gravity="center"
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
android:layout_width="match_parent"
android:gravity="left|center" // Text will Center & left align
仅供参考。
您正在使用 RelativeLayout 。对于您的 TEXTVIEW Vertical 问题,您应该使用 android:layout_below.
Positions the top edge of this view below the given anchor view ID. Accommodates top margin of this view and bottom margin of anchor view.
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center" // settext gravity as per your requirement
android:text="Happy"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:gravity="center"// settext gravity as per your requirement
android:text="Birthday"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center" // settext gravity as per your requirement
android:text="To You"
android:textAppearance="?android:textAppearanceLarge" />
</RelativeLayout>
使用layout_centerVertical="true"
在垂直方向居中视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Happy"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Birthday"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="To You"
android:textAppearance="?android:textAppearanceLarge" />
</RelativeLayout>
只需在父布局中使用android:gravity="center"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Happy" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="Birthday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:textAppearance="?android:textAppearanceLarge"
android:text="To You" />
</RelativeLayout>
您的文件现在生成的是:
我猜你想要的是这个:
所以只需在您的根布局中添加以下行,这会将其所有子视图在其中心水平对齐
android:gravity="center_horizontal"