【问题标题】:How to make the button fit the screen?如何使按钮适合屏幕?
【发布时间】:2016-03-03 23:02:45
【问题描述】:

我正在尝试制作一个应用,但是当我在不同设备上对其进行测试时,按钮的大小保持不变。我尝试使用具有等于 5dp 的填充的 Wrap_Content。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@ id/img2"


        />

    <Button
        android:layout_width="192dp"
        android:layout_height="wrap_content"
        android:id="@ id/button"
        android:text="wallpaper"
        android:alpha="0.1"
        android:layout_gravity="left|bottom"
         />

    <Button
        android:layout_width="192dp"
        android:layout_height="wrap_content"
        android:text="back"
        android:id="@ id/button2"
        android:layout_gravity="right|bottom"
        android:alpha="0.1" />



</FrameLayout>

我希望两个按钮都在底部中心相遇,但并非所有设备都符合预期。这是我设置 Match_Parent 时发生的情况

【问题讨论】:

  • 你到底想要什么??

标签: android xml android-layout android-studio


【解决方案1】:

如果您希望按钮并排排列并适合所有屏幕尺寸和方向,您可以在线性布局上使用权重来完成它。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android=" http://schemas.android.com/apk/res /android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@ id/img2"/>
<LinearLayout android:width="match_parent"
android:height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:id="@ id/button"
android:text="wallpaper"
android:alpha="0.1"/>

<Button android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:text="back"
android:id="@ id/button2"
android:alpha="0.1"/>
</LinearLayout>
</FrameLayout>

不要被尺寸 0dp 吓到。 weight 属性用于根据比率分配大小。这里两个按钮的重量都是 0.5。所以两者都相等地占据整个宽度。由于计算了宽度,因此无需指定尺寸。所以设置为0。

【讨论】:

  • 很高兴为您提供帮助。希望这是您的预期。
  • 我也在寻找一些东西来删除空格,它工作得很好,谢谢。
【解决方案2】:

试试这个代码 -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout         
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <Button
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/button"
        android:text="wallpaper"
        android:alpha="0.1" />

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#fff" />

    <Button
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="back"
        android:id="@+id/button2"
        android:alpha="0.1" />

</LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案3】:

    使用这个:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000">
    
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/img2" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                    <Button
                        android:layout_weight="1" <!-- add weight -->
                        android:layout_width="0dp" <!-- change 192dp to 0dp -->
                        android:layout_height="wrap_content"
                        android:id="@+id/button"
                        android:text="wallpaper"
                        android:alpha="0.1"
                        android:layout_gravity="left|bottom" />
                    <Button
                        android:layout_weight="1" <!-- add weight -->
                        android:layout_width="0dp" <!-- change 192dp to 0dp -->
                        android:layout_height="wrap_content"
                        android:id="@+id/button2"
                        android:text="back"
                        android:alpha="0.1"
                        android:layout_gravity="left|bottom" />
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>
    

    【讨论】:

    • 它显示错误,因为应该为高度和宽度定义属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2015-01-12
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多