【问题标题】:Centering borderless buttons between a horizontal line在水平线之间居中无边框按钮
【发布时间】:2013-10-19 13:34:30
【问题描述】:

我正在尝试使用 android 中的边框按钮。目前我有一个按钮看起来像这样的活动:

按钮由一条垂直线分隔,我希望它们位于由垂直线创建的“列”的中心:

这是我的 xml:

    <?xml version="1.0" encoding="utf-8"?>

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <!-- Card Contents go here -->
            <TextView
                android:id="@+id/breweryTitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="20sp"
                android:textStyle = "bold"
                android:padding="5dip"
                >
            </TextView>




        </LinearLayout >

    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <!-- Card Contents go here -->


            <ImageView android:id="@+id/breweryImage"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_margin="10dip"/>


        </LinearLayout >

    </FrameLayout>





<TableLayout  

android:id="@+id/tableLayout2"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:layout_gravity="center_horizontal"
android:shrinkColumns="*"  
android:stretchColumns="*">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <!-- Card Contents go here -->

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

            <TextView
                android:id="@+id/breweryEstablished"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:text="Year Established: "
                android:textSize="15sp"
                android:textStyle = "bold"
                android:padding="5dip"
                >
            </TextView>

            <TextView
                android:id="@+id/yearTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:text=""
                android:textSize="15sp"
                android:padding="5dip"
                >
            </TextView>

            </LinearLayout>


        </LinearLayout >

    </FrameLayout>





</TableLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <!-- Card Contents go here -->
            <TextView
                android:id="@+id/beerDescriptionTitle2"
                android:textStyle = "bold"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="15sp"
                android:text="Description:"
                android:padding="5dip"
                ></TextView>

            <TextView
                android:id="@+id/breweryDescription"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="15sp"
                android:padding="5dip"

                ></TextView>





        </LinearLayout >

    </FrameLayout>



    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="vertical"
            android:background="@drawable/bg_card">

            <TextView
                android:id="@+id/discoverLabel"
                android:textStyle = "bold"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="15sp"
                android:text="Discover:"
                android:padding="5dip"
                ></TextView>

            <!-- Card Contents go here -->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:orientation="horizontal"
                >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="All Beers"
                    android:id="@+id/button"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:onClick="getAllBeers"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1"/>

                <View
                    android:id="@+id/ViewColorPickerHelper"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="4dip"
                    android:layout_marginTop="4dip"
                    android:background="?android:attr/dividerVertical"
                    android:layout_centerHorizontal="true"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Top Beers"
                    android:id="@+id/button2"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:onClick="getTopBeers"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1"
                    />

                <View
                    android:id="@+id/ViewColorPickerHelper"
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="4dip"
                    android:layout_marginTop="4dip"
                    android:background="?android:attr/dividerVertical"
                    android:layout_centerHorizontal="true"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Your Beers"
                    android:id="@+id/button2"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:onClick="getYourTopBeers"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1"
                    />

            </LinearLayout>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Directions to Brewery"
                android:id="@+id/button2"
                style="?android:attr/borderlessButtonStyle"
                android:textColor="@color/orange"
                android:textStyle="bold"
                android:onClick="getDirections"
                />





        </LinearLayout >

    </FrameLayout>





</LinearLayout>

我尝试过使用布局宽度和重力,但仍然没有成功。

更新:

我尝试了最近的建议,现在我将它们全部正确居中,但这只会使所有内容都变得超长,而且我看不到倒数第三个按钮。

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">

                <TextView
                    android:id="@+id/discoverLabel"
                    android:textStyle = "bold"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:textSize="15sp"
                    android:text="Discover:"
                    android:padding="5dip"
                    ></TextView>

                <!-- Card Contents go here -->
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    android:orientation="horizontal"
                    >


                    <Button
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="All Beers"
                        android:id="@+id/button"
                        style="?android:attr/borderlessButtonStyle"
                        android:textColor="@color/orange"
                        android:textStyle="bold"
                        android:onClick="getAllBeers"
                        android:layout_centerHorizontal="true"
                        android:layout_weight="0.33"/>

                    <View
                        android:id="@+id/ViewColorPickerHelper"
                        android:layout_width="1dip"
                        android:layout_height="match_parent"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="4dip"
                        android:layout_marginTop="4dip"
                        android:background="?android:attr/dividerVertical"
                        android:layout_centerHorizontal="true"/>

                    <Button
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="Top Beers"
                        android:id="@+id/button2"
                        style="?android:attr/borderlessButtonStyle"
                        android:textColor="@color/orange"
                        android:textStyle="bold"
                        android:onClick="getTopBeers"
                        android:layout_weight="0.33"
                        />

                    <View
                        android:id="@+id/ViewColorPickerHelper"
                        android:layout_width="1dip"
                        android:layout_height="match_parent"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="4dip"
                        android:layout_marginTop="4dip"
                        android:background="?android:attr/dividerVertical"
                        android:layout_centerHorizontal="true"/>

                    <Button
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="Your Beers"
                        android:id="@+id/button2"
                        style="?android:attr/borderlessButtonStyle"
                        android:textColor="@color/orange"
                        android:textStyle="bold"
                        android:onClick="getYourTopBeers"
                        android:layout_centerHorizontal="true"
                        android:layout_weight="0.33"
                        />


                </LinearLayout>

                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Directions to Brewery"
                    android:id="@+id/button2"
                    style="?android:attr/borderlessButtonStyle"
                    android:textColor="@color/orange"
                    android:textStyle="bold"
                    android:onClick="getDirections"
                    />



            </LinearLayout >

        </FrameLayout>

我还尝试将其线性布局更改为 wrap-content 的高度,但这并没有解决它。

【问题讨论】:

    标签: android android-layout android-xml android-button


    【解决方案1】:

    包裹按钮的线性布局应该有layout_widthmatch_parent,而不是wrap content。这样,它将占用所有可用空间。

    我们所拥有的基本上如下图所示:layout_weight 根据提供的数量将布局的额外空间分配给子项。由于 width 被告知要包裹它的孩子,所以没有额外的空间。如果我们给一个更大的尺寸,比如match_parent,就会有额外的空间,这样额外的空间就会根据给layout_weight的数字分配给孩子们。在这种情况下,它将为每个按钮分配相等的数量,因此它们的大小相同。

    【讨论】:

      【解决方案2】:

      尝试使用 center_horizo​​ntal 而不是 center 作为重力值。重量也意味着是一个浮点数,因为你想要每个宽度的 1/3,你应该使用 0.33 而不是 1。如果你设置 layout_width="0",你只能水平使用 layout_weight,所以也这样做!正如 moveaway00 所说,您应该确保父布局使用的是 match_parent 而不是 wrap_content。

      总结:有一堆错误,使用我在下面的cmets中提到的背景颜色技巧,以便将来有条不紊地调试此类问题;)

      【讨论】:

      • 那种只是出于某种原因将文本向上移动。
      • 尝试检查每个按钮的实际大小,方法是将它们的背景设置为不同的、明显的颜色。另外,如果布局中没有那些 1dp 宽的视图,问题是否存在?
      • 我有我想要的另一个活动的工作,我把工作的屏幕截图和 xml 代码放在一起。我不明白为什么这个不同。
      • 我有一种预感,这 3 个按钮每个都是文本的大小(换行)而不是屏幕宽度的 1/3,您可以通过将背景设置为绿色来检查这一点。
      • 几乎修复了它。它使按钮正确排列,但现在页面超长。我粘贴了更改后的 xml、屏幕截图以及我试图解决此问题的内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 2020-03-14
      • 2017-07-29
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多