如何使按钮适合屏幕?

时间:2016-03-03 14:28:06

标签: android xml android-layout android-studio

我正在尝试创建一个应用程序,但是当我在不同的设备上测试时,按钮的大小保持不变。我尝试使用Wrap_Content,填充等于5dp。这是代码:

<?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

时发生的情况

3 个答案:

答案 0 :(得分: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的大小吓跑。权重属性用于根据比率分配大小。这两个按钮都有0.5重量。所以两者都占据整个宽度。由于计算宽度,因此无需指定大小。所以它设置为0。

答案 1 :(得分:0)

使用此:

<?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>

答案 2 :(得分:0)

试试这段代码 -

<?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>
相关问题