[英]Android layout_gravity Bottom/Top in LinearLayout
在堆栈溢出时,有数百万个与“ layout_gravity底部/顶部LinearLayout中的顶部”相关的问题和答案,但是我仍然没有解决我的问题。
我想在我的EditText的最顶部放置“请把我放在最顶部”,在TextView的最底部放置“请把我放在最底部”。 我的梦想是非常基本的,但我无法实现!
谁能帮我?
这是我的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:background="@android:color/holo_green_light"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Please place me at very top" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/background_light"
android:text="TextView"
android:layout_weight="0.19" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_weight="0.70"
android:background="@color/colorAccent"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_gravity="top"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:background="@android:color/background_light"
android:text="Please place me at very bottom" />
</LinearLayout>
</LinearLayout>
这是我的输出:
您已将重力底部设置为第二个线性布局,以将视图放置在底部。
首先更改LinearLayout的这一行:
android:gravity="center_vertical"
至:
android:gravity="top"
在Second LinearLayout上更改此行:
android:gravity="center_vertical"
至:
android:gravity="bottom"
注意:避免使用嵌套权重,这会降低性能。
如果LinearLayout无法正常工作,请不要强迫自己使用它。
嵌套的LinearLayouts和权重不利于性能。
RelativeLayout(或ConstraintLayout)自然是用来将事物放置在ViewGroups的锚点上的
我假设您希望文本和按钮基于其他属性居中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/holo_green_light"
android:layout_weight="1">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_alignParentTop="true"
android:inputType="textPersonName"
android:text="Please place me at very top" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/background_light"
android:text="TextView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_centerInParent="true"
android:text="Button"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/background_light"
android:text="Please place me at very bottom"/>
</RelativeLayout>
</LinearLayout>
另外,您只需要在第二个LinearLayout上使用android:gravity="bottom"
,那么其中的所有视图都在底部。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<!-- Top Gravity -->
<LinearLayout
android:gravity="top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/holo_green_light"
android:orientation="vertical"
android:layout_weight="0.5">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please place me at very top"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="TextView"/>
</LinearLayout>
<!-- Bottom Gravity -->
<LinearLayout
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="62dp"
android:text="Button"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="Please place me at very bottom"/>
</LinearLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.