【问题标题】:Dynamically adjust position of Button in Android Studio在 Android Studio 中动态调整 Button 的位置
【发布时间】:2016-06-24 06:12:32
【问题描述】:

当我在 Activity xml 的设计视图中看到时,我在线性布局中有一个按钮,它看起来位置完美,但是当我在 GenyMotoin avd 中看到它时,它与我的预期相差甚远。 如何动态调整按钮在不同屏幕尺寸上的位置?

我的 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>

【问题讨论】:

  • 你的问题是?
  • 1.请具体说明您的问题。 2. 发布您的 xml 文件。 3. 你想达到什么目标,现在得到了什么?
  • 你到现在都做了什么?

标签: android android-layout android-button


【解决方案1】:

对最外层的布局使用线性布局。

【讨论】:

    【解决方案2】:

    目前尚不清楚您要实现什么目标,但如果您想在所有设备上将视图置于中心位置。尝试使用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>
    

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 2021-01-21
      • 2021-04-13
      • 2021-07-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      相关资源
      最近更新 更多