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

时间:2014-11-24 09:09:43

标签: android android-layout android-xml android-library

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

This is how my layout looks with the library

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

enter image description here

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="abc@example.com"
    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>

1 个答案:

答案 0 :(得分: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"/>

对于单个按钮,只需使用宽度为&#34; match_parent&#34;

<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" />

希望这有帮助。