定义"VIEW"位置的准确方法是什么?



到目前为止,我试图使用"android:layout_marginTop"定义视图的位置,这在当时似乎更容易,但现在我发现了严重的问题。 最底部的视图不会显示在较小的屏幕上。

这是我的 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context=".StartActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#6CD9CE">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:scaleX="1.0"
android:scaleY="1.0"
android:src="@drawable/logo_transparent" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome."
android:textAlignment="center"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:textStyle="bold"/>

<Button
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff"
android:backgroundTint="#6CD9CE"
android:layout_marginTop="60dp"
android:text="Login"
android:textColor="@color/white"/>

<Button
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#6CD9CE"
android:layout_marginTop="50dp"
android:text="Register"
android:textColor="@color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RandomText"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="200dp"/>

</LinearLayout>

模拟器和设计预览的屏幕截图:

设计预览

仿真

您可以使用"weight"属性或尝试使用ScrollView

你应该使用ContraintLayout但是如果你不想做大的改变,你可以将最后一个TextView设置为有android:layout_height="0dp"android:layout_weight="1",这样它将占用所有其余的LinearLayout,设置android:gravity="bottom",然后用android:paddingBottom="20dp"从底部稍微间隔一下

喜欢这个:

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="bottom"
android:paddingBottom="20dp"
android:text="RandomText"
android:textSize="20sp" />

干杯

相关内容

最新更新