动态调整Android Studio中按钮的位置



我在活动xml的设计视图中看到一个线性布局的按钮,看起来它是完美的定位预期的。如何动态调整不同屏幕尺寸的按钮位置?

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xxx.MainScreen"
    android:background="#b6b6b6">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="263dp"
            android:id="@+id/textView"
            android:textSize="50dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="264dp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next Act"
            android:id="@+id/button"
            android:layout_marginTop="100dp"
            android:layout_marginLeft="20dp" />
    </LinearLayout>
</RelativeLayout>

在最外面的布局中使用线性布局。

尚不清楚您要实现的目标,但是如果您想将视图放在所有设备上的中心。尝试使用RelativElayout而不是Linearlayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="#b6b6b6"
    android:padding="@dimen/activity_vertical_margin"
    android:weightSum="2"
    android:orientation="vertical"
    tools:context="com.offtogeek.motivatemyself.MainScreen">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="left"
            android:textSize="50dp" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:text="Next Quote" />
    </RelativeLayout>
</LinearLayout>

最新更新