【问题标题】:How to create a layout containing a textview with buttons on either side如何创建包含文本视图的布局,两侧有按钮
【发布时间】:2012-03-08 13:35:19
【问题描述】:

如何在文本视图的任一侧创建一个带有按钮的布局?这是我到目前为止所得到的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/left_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <TextView
        android:id="@+id/center_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:padding="10dip"
        android:text="textview" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />
</RelativeLayout>

这里的问题是文本视图居中。我希望 textview 填充剩余的完整空间。我尝试将其 android:layout_width 设置为 fill_parent 并删除 android:layout_centerHorizo​​ntal,但它似乎与按钮重叠。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    第一步

    您需要在屏幕左侧创建第一个按钮提及android:layout_alignParentLeft="true"

    第二步

    在屏幕右侧创建第二个按钮提及android:layout_alignParentRight="true"

    第三步

    现在创建 TextView 并通过提及在第一个按钮的右侧和第二个按钮的左侧提及它

    android:layout_toRightOf="@id/left_button"
        android:layout_toLeftOf="@+id/right_button"
    

    XML 代码

    <?xml version="1.0" encoding="utf-8"?>
    
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/left_button"
                android:layout_width="72dip"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="left" />
    
            <Button
                android:id="@+id/right_button"
                android:layout_width="72dip"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="right" />
            <TextView
                android:id="@+id/center_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_toRightOf="@id/left_button"
                android:layout_toLeftOf="@+id/right_button"
                android:padding="10dip"
                android:text="textview" />
    
    
        </RelativeLayout>
    

    【讨论】:

    • 这似乎解决了问题。非常感谢!
    【解决方案2】:

    听起来你会更好地使用 LinearLayout,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/left_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="left" />
    
        <TextView
            android:id="@+id/center_textview"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_weight="1"
            android:padding="10dip"
            android:text="textview" />
    
        <Button
            android:id="@+id/right_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="right" />
    </RelativeLayout>
    

    这样三个视图都将位于一条水平线上,并且 TextView 将在按钮布局后占用任何剩余空间(请参阅 android:layout_weight 属性)。

    【讨论】:

    • 这有助于更好地理解 android:layout_weight 属性!会记住的。
    【解决方案3】:

    这些属性可能会解决你的问题

    android:layout_toLeftOf
    android:layout_toRightOf
    

    【讨论】:

      【解决方案4】:

      尝试使用带有orientation="horizo​​ntal"的LinearLayout,而不是RelativeLayout。

      或者如果你想做RelativeLayout,你可以使用Chandra提到的属性(即,相对于按钮对齐textview,而不是相对于父容器)。

      【讨论】:

        【解决方案5】:

        不确定你想要什么,但你可以使用例如。 layout_toRightOf。将按钮与父级左侧对齐,然后将视图放置在左侧视图右侧的示例。

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        
        <Button
            android:id="@+id/left_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="left" />
        
        <TextView
            android:id="@+id/center_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_toRightOf="@id/left_button"
            android:padding="10dip"
            android:text="textview" />
        
        <Button
            android:id="@+id/right_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/center_textview"
            android:text="right" />
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-26
          • 2016-09-30
          • 1970-01-01
          • 1970-01-01
          • 2020-01-18
          • 2019-06-02
          • 1970-01-01
          相关资源
          最近更新 更多