【问题标题】:Android Buttons not stretching to width of the screenAndroid Buttons 未拉伸到屏幕宽度
【发布时间】:2015-01-21 22:50:15
【问题描述】:

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

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

XML 代码

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

【问题讨论】:

  • 你需要拉伸哪一个?好的/取消还是检查?
  • @androidKiller 他们俩实际上.. 我希望“确定/取消”并排(即相同大小并填充宽度)​​并“检查”以填充屏幕的宽度.
  • 尝试使用 dp 代替主要用于文本大小的 sp。

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


【解决方案1】:

如果使用 weight 属性,则需要将宽度用作 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"/>

对于单个按钮,只需将宽度用作“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" />

希望这会有所帮助。

【讨论】:

  • 它不起作用..我在上面添加了我添加代码时发生的屏幕截图
  • @MysticMagic 我认为这是图书馆本身的问题。我知道您的代码非常适合普通按钮,所以我会将其标记为正确答案。
猜你喜欢
  • 2015-09-03
  • 2019-07-11
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
相关资源
最近更新 更多