如何在屏幕中央堆叠两个Android元素
我希望编辑文本和按钮堆叠在屏幕中央的彼此顶部。很明显,我缺少一个非常简单的方法。在这里,它们粘在屏幕的左上角。任何帮助都将不胜感激。我也尝试过相对布局,但它们在Z轴上彼此重叠如何在屏幕中央堆叠两个Android元素,android,Android,我希望编辑文本和按钮堆叠在屏幕中央的彼此顶部。很明显,我缺少一个非常简单的方法。在这里,它们粘在屏幕的左上角。任何帮助都将不胜感激。我也尝试过相对布局,但它们在Z轴上彼此重叠 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_heigh
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
>
<EditText
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<Button
android:text="Go >"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
我建议使用
RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
使用相对布局,将元素放在彼此的顶部:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<EditText
android:id="@+id/tv_main"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button_main"
android:text="Go >"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@id/tv_main"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
只需在布局中使用安卓:gravity=“center”,它就能为您完成任务
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<EditText
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:text="Go Went Gone"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout>
尝试以下数据
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
>
<EditText
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
/>
<Button
android:text="Go >"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
/>
</LinearLayout>
您可以将编辑文本和按钮包装成垂直布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/hello_world" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Go >" />
</LinearLayout>
很好的解决方案,虽然不完全在屏幕中央,但这种布局的深度较低,这是一件好事;)如果答案是正确的,请为其他用户接受它。如果没有,请添加一些我不理解的细节“此布局深度较低”。我不是原始海报,但我想说的是,拥有平面视图层次结构是一种很好的做法-在您的布局中就是这样。与此类似,元素精确居中,但布局具有更大的视图层次深度。看起来OP自己找到了答案,忘记了他的问题;)那个布局是这样的。。。我希望它也在屏幕的中心垂直。嘿,谢谢,我改变了你的答案,以填补家长家长然后它的工作。谢谢