如何设置按钮适合屏幕

时间:2016-03-08 18:13:24

标签: android android-layout

我正在尝试制作布局,顶部是Imageview,底部是按钮。我使用了weight属性来设置按钮和图像的固定比例。但是我无法设置底部按钮所以填补空洞内容。我希望按钮的比例为50-50。这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_weight="0.1"/>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="left|bottom"
        android:layout_weight="0.9"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_gravity="right|bottom"
        android:layout_weight="0.9"
        />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

要设置底部按钮以使水平适合内容,应将Linearlayout设为match_parent。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="left|bottom"
        android:layout_weight="0.9"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_gravity="right|bottom"
        android:layout_weight="0.9"
        />

</LinearLayout>

答案 1 :(得分:0)

现在只需检查代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0.1" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:layout_weight="0.1"
        android:text="New Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="right|bottom"
        android:layout_weight="0.1"
        android:text="New Button" />

</LinearLayout>
相关问题