Android按钮没有拉伸到屏幕的宽度

问题描述 投票:0回答:1

我正在使用android的bootstrap库,可以找到here。我已经将库导入我的应用程序,但问题是,按钮不会跨越整个屏幕宽度。我尝试并排放置按钮并使用android:layout_weight="1",但按钮只是向左移动而不伸展。我甚至尝试过使用android:layout_width="match_parent",但这也增加了按钮的高度。

如果我设置android:layout_width="0dip"android:layout_weight="1"(用于确定/取消)和android:layout_width="match_parent"(用于检查按钮)会发生这种情况

XML code

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="318dp"
    android:layout_height="wrap_content"
    android:text="Send Pin To Email id."
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enter Your Email Id Below"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<com.beardedhen.androidbootstrap.BootstrapEditText
    android:id="@+id/et_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:ems="10"
    android:hint="[email protected]"
    android:inputType="textEmailAddress"
    bootstrapbutton:be_roundedCorners="true" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingBottom="15sp" >

    <com.beardedhen.androidbootstrap.BootstrapButton
        android:id="@+id/OK"
        style="@style/AppBaseTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="OK"
        bootstrapbutton:bb_icon_left="fa-envelope"
        bootstrapbutton:bb_roundedCorners="true"
        bootstrapbutton:bb_size="medium"
        bootstrapbutton:bb_type="default" />

    <com.beardedhen.androidbootstrap.BootstrapButton
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="CANCEL"
        bootstrapbutton:bb_icon_left="fa-times"
        bootstrapbutton:bb_roundedCorners="true"
        bootstrapbutton:bb_size="medium"
        bootstrapbutton:bb_type="default" />
</LinearLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="15sp"

    android:text="Got your Pin?Enter it below."
    android:textAppearance="?android:attr/textAppearanceMedium" />

<com.beardedhen.androidbootstrap.BootstrapEditText
    android:id="@+id/et_pin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:ems="10"
    android:hint="XXXX"
    android:inputType="number"
    bootstrapbutton:be_roundedCorners="true" />

<com.beardedhen.androidbootstrap.BootstrapButton
    android:id="@+id/b_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:text="Check"
    bootstrapbutton:bb_icon_left="fa-check"
    bootstrapbutton:bb_roundedCorners="true"
    bootstrapbutton:bb_size="medium"
    bootstrapbutton:bb_type="default" />

</LinearLayout>
android android-layout android-xml android-library
1个回答
1
投票

如果使用重量属性,则需要使用宽度作为0dip而不是wrap_content

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingBottom="15sp" 
    android:weightSum="2">
    <com.beardedhen.androidbootstrap.BootstrapButton
        android:id="@+id/OK"
        style="@style/AppBaseTheme"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="OK"
        bootstrapbutton:bb_icon_left="fa-envelope"
        bootstrapbutton:bb_roundedCorners="true"
        bootstrapbutton:bb_size="medium"
        bootstrapbutton:bb_type="default" 
        android:layout_weight="1"/>

    <com.beardedhen.androidbootstrap.BootstrapButton
        android:id="@+id/cancel"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="CANCEL"
        bootstrapbutton:bb_icon_left="fa-times"
        bootstrapbutton:bb_roundedCorners="true"
        bootstrapbutton:bb_size="medium"
        bootstrapbutton:bb_type="default"
        android:layout_weight="1"/>

对于单个按钮,只需使用width作为“match_parent”

<com.beardedhen.androidbootstrap.BootstrapButton
    android:id="@+id/b_check"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:text="Check"
    bootstrapbutton:bb_icon_left="fa-check"
    bootstrapbutton:bb_roundedCorners="true"
    bootstrapbutton:bb_size="medium"
    bootstrapbutton:bb_type="default" />

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.