【问题标题】:Three buttons evenly divided linear layout三个按钮均分线性布局
【发布时间】:2016-07-05 02:23:34
【问题描述】:

所以,我尝试使用线性布局来容纳三个不同的按钮,每个按钮应占一行宽度的 33%。此线性布局将位于我的相对布局中的所有其他内容下方,该布局包含此活动上的所有其他小部件。不幸的是,当我将第三个按钮添加到布局中时,其他两个按钮的底部有一个白条,第三个按钮(在本例中为主页按钮)的位置高于其他按钮。

有人可以解释这种行为以及如何纠正它吗?谢谢。

这是线性布局的 XML 文件,我已经删除了其他小部件的所有文本。如果这有帮助,我也可以发布它。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">

    <Button
        android:layout_width="0dp"
        android:layout_weight=".33"
        android:layout_height="wrap_content"
        android:text="@string/adfazsdfasdfadfsagjlfkdlgjklfsadgfjgps"
        android:onClick="resetDates"
        android:background="@drawable/sumbitstyleing"
        android:id="@+id/resetDatesButton" />

    <Button
        android:layout_width="0dp"
        android:layout_weight=".33"
        android:layout_height="wrap_content"
        android:text="@string/home"
        android:onClick="home"
        android:id="@+id/homeButtonSearch"
        android:background="@drawable/generalbutton" />

    <Button
        android:layout_weight=".33"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/submitchanges"
        android:onClick="submitChanges"
        android:background="@drawable/sumbitstyleing"
        android:id="@+id/submitchanges" />

</LinearLayout>

第一张图没有第三个按钮,第二张图有第三个按钮。

【问题讨论】:

  • 不要使用 alignParentEnd 和 alignParentRight。首先,这两者在 LTR 语言中会发生冲突。其次,由于您在宽度上 match_parent,您将自动对齐到父级的两侧。 (alignParentBottom 很好,不会冲突)。
  • 将android:baselineAligned="false" 添加到LinearLayout 以使Buttons 排队。我不确定白线是什么。某种渲染故障?这是在模拟器中,还是在布局设计师中?

标签: android button android-linearlayout


【解决方案1】:

试试这个,我删除了你应该与RelativeLayout一起使用的属性,而与LinearLayout无关的属性,以及水平对齐的按钮,

    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"

并将 Weightsum 与按钮高度一起分配给 LinearLayout 以匹配父项

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:weightSum="1">

        <Button
            android:id="@+id/resetDatesButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".33"
            android:onClick="resetDates"
            android:text="Rese check dates" />

        <Button
            android:id="@+id/homeButtonSearch"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".33"
            android:onClick="home"
            android:text="home" />

        <Button
            android:id="@+id/submitchanges"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".33"
            android:onClick="submitChanges"
            android:text="submit changes" />

    </LinearLayout>

</RelativeLayout>

结果

【讨论】:

    【解决方案2】:

    你可以这样走:

     <?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="@color/white"
    >
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerHorizontal="true"/>
        <!--Buttons -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            >
    
            <android.support.v7.widget.AppCompatButton
                android:id="@+id/motor_team_send_sms_btn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/send_sms"
    
                />
    
            <android.support.v7.widget.AppCompatButton
                android:id="@+id/motor_team_sleep_btn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/sleep"
    
                />
    
            <android.support.v7.widget.AppCompatButton
                android:id="@+id/motor_team_rise_btn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/rise"
    
                />
        </LinearLayout>
    

    输出是:

    这可能会对你有所帮助。

    【讨论】:

      【解决方案3】:

      试试这个,在你的 LinearLayout 中添加 android:gravity="center"

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:gravity="center">
      ...
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2011-01-18
        • 1970-01-01
        • 2014-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 2014-08-11
        • 2015-08-11
        相关资源
        最近更新 更多