[Android]如何创建一个View的分割线

本文介绍了在Android中创建View分割线的两种方法:1) 通过人工添加LinearLayout的View来作为分割线;2) 在LinearLayout中定义divider属性。针对API 11及以上版本,可以通过设置LinearLayout的style来显示默认的分割线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

buttons-with-separators3

在这里插入图片描述

如何创建一个View的分割线,如上图

我们见介绍三种可以创建看起来很不错的view的分割线,如在button之间添加分割线。

这个例子是将为LinearLayout内的三个Button间添加分割线。

 这三个例子可能容易实现,相信会有更好的实现办法。

buttons-with-separators3
在这里插入图片描述

1 人工添加LinearLayout的分割线

我们可以创建一个View,这个View就是分割线,只要简单在Button之间添加这个分割线就可以。

分割线的实现,如下:

<View
 
 android:layout_height="fill_parent"
 
 android:layout_width="1dp"
 
 android:background="#90909090"
 
 android:layout_marginBottom="5dp"
 
 android:layout_marginTop="5dp"
 
/>

So the whole layout, as pictured, becomes:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:orientation="horizontal">
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Yes"
        android:layout_weight="1"
        android:id="@+id/button1"
        android:textColor="#00b0e4" />
  
    <View android:layout_height="fill_parent"
        android:layout_width="1px"
        android:background="#90909090"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:id="@+id/separator1" />
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="No"
        android:layout_weight="1"
        android:id="@+id/button2"
        android:textColor="#00b0e4" />
  
    <View android:layout_height="fill_parent"
        android:layout_width="1px"
        android:background="#90909090"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:id="@+id/separator2" />
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Neutral"
        android:layout_weight="1"
        android:id="@+id/button3"
        android:textColor="#00b0e4" />
  
</LinearLayout>

2 在LinearLayout定义divider
你可以给LinearLayout设置a view divider,这很明显是个很好的解决方法,尤其是不知道LinearLayout下有多少个子Button。

这种必须是在API level 11 或者更高的API版本使用。

我们先定义这个分割线样式吧:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <size android:width="1dp" />
 <solid android:color="#90909090" />
</shape>

把这个分割线的样式设置给LinearLayout

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:divider="@drawable/separator"
    android:showDividers="middle"
    android:orientation="horizontal">
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Yes"
        android:layout_weight="1"
        android:id="@+id/button1"
        android:textColor="#00b0e4" />
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="No"
        android:layout_weight="1"
        android:id="@+id/button2"
        android:textColor="#00b0e4" />
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Neutral"
        android:layout_weight="1"
        android:id="@+id/button3"
        android:textColor="#00b0e4" />
  
</LinearLayout>

其中最重要当然就是:

 android:divider="@drawable/separator"
android:showDividers="middle" 

As danialgoodwin mentioned in the comments, adding the buttonBarStyle to the LinearLayout will show default separators. This is also for api level 11 or higher only.

The important part here, is adding this line to the LinearLayout:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="?android:buttonBarStyle"
    android:dividerPadding="15dp"
    >
  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button1"
        android:layout_gravity="center_vertical" />
  
    <!-- more buttons/views -->
      
</LinearLayout>
博客
v8worker
05-08 2949
05-06 2933
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值