线性布局中的无边框按钮,安卓中的layout_weight



这是布局代码

 <LinearLayout
    android:id="@+id/mapOptions"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:background="@color/white" >
    <View
        android:id="@+id/mapOptionMenuShade"
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="?android:attr/dividerVertical" />
    <Button
        android:id="@+id/BtnMap"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="?android:attr/selectableItemBackground"
        android:text="@string/map" />
    <View
        android:id="@+id/Shade2"
        android:layout_width="1dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_marginTop="4dip"
        android:background="?android:attr/dividerVertical" />
    <Button
        android:id="@+id/BtnSatellite"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="?android:attr/selectableItemBackground"
        android:text="@string/satellite" />
    <View
        android:id="@+id/Shade3"
        android:layout_width="1dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_marginTop="4dip"
        android:background="?android:attr/dividerVertical" />
    <Button
        android:id="@+id/BtnHybrid"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="?android:attr/selectableItemBackground"
        android:text="@string/hybrid" />
</LinearLayout>

但是有了这些"视图",我无法显示我的按钮。

谷歌的无主之地按钮示例的链接。这就是为什么我暗示了这些观点。

在这里,我想显示填充整个线性布局的 3 个无边框按钮


|-1--2-|

-3-||----|----|----|

谢谢

您可以使用任何视图并将 onClickListener 附加到它,而不是使用Button。因此,如果您想要无边框按钮,请使用TextView代替,您将获得所需的。尝试:

<TextView
    android:id="@+id/BtnSatellite"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:text="@string/satellite" />

而不是你的

<Button
    android:id="@+id/BtnSatellite"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:text="@string/satellite" />

(基本上从用TextView替换Button开始)。如果您调用findViewById()并将结果转换为Button请记住调整您的代码,如果您不更新它,您将获得强制转换异常。

编辑

开发人员的文档"无边框按钮"中描述了替代方法,但是它需要至少运行API11(Honeycomb)的设备,因此请明智地使用。

若要创建无边框按钮,需要将以下内容添加到按钮的 XML 代码中。这是 API 11(蜂窝)的新增功能

style="?android:attr/borderlessButtonStyle"

或者,您可以简单地使用文本视图或图像视图作为按钮。

最新更新