【发布时间】:2017-09-10 22:40:26
【问题描述】:
关于堆栈溢出的“layout_gravity Bottom/Top in 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>
这是我的输出:
【问题讨论】:
-
为什么不直接使用 ConstraintLayout?
-
换句话说,在使用权重时留下
android:layout_height通常是错误的。另外,您已经应用了重心,所以不清楚为什么任何东西应该在顶部或底部。 -
因为这是一个使用 LinearLayout 创建的屏幕,我不想更改所有屏幕。我只想添加一个按钮。 (这个按钮应该放在底部)
-
我没有说要改变所有屏幕,只有这个
标签: android android-layout layout-gravity