How to create simple view separators

创建Android应用中按钮之间的美观分隔符
本文介绍如何在Android应用布局中创建简洁、美观的分隔符,用于在一组按钮之间增加视觉区分。提供了三种实现方法,包括手动添加视图、设置线性布局的分隔线和使用按钮样式默认分隔。这些方法适用于不同API级别,旨在帮助开发者轻松定制UI设计。
CV from http://envyandroid.com/archives/1193/view-separators

How to create simple view separators

This will show you three ways to create nice looking view separators, to use between a row of buttons for example.

The examples shows a LinearLayout with three buttons, that should get separators in between them.
The third example may be the easiest one to implement, while the other ones gives you better customizability.

buttons-with-separators3

 

 

Method one – Manually adding a view separator to the LinearLayout

We can create a simple View that represents the separator, and just add it in between our buttons.

As a separator, we can use a view like this:

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

Method two – Specifying a divider for the LinearLayout

As pointed out by Paul Burke in the comments, you can specify a view divider on the linearlayout. This is obviously a better solution (and the solution I was searching for), that also works when you are dealing with an unknown number of items.

This methods only works for API level 11 and higher.

Create a new file called separator.xml in the drawable folder:

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

We can then use this separator in our LinearLayout like this:

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

The important part here is on the LinearLayout:

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

 

Method three – ButtonBarStyle (the easiest way for default separators)

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:

style="?android:buttonBarStyle"

And the whole code will look like this:

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

You can also adjust the paddings of the view separators with the “dividerPadding” setting.


<think> 嗯,用户遇到了IntelliJ IDEA在Git提交时提示"you are about to commit CRLF line separators"的问题。这确实是个常见的跨平台协作问题。 用户可能正在Windows环境下开发,但项目中有其他贡献者使用Linux/macOS系统。这个警告的本质是不同操作系统换行符标准不同引发的兼容性问题。 从引用[1]看,这是CRLF(Windows)和LF(Unix)行尾符不匹配导致的。Git为了保持仓库一致性,会触发这个警告。用户的核心诉求应该是消除警告并正确配置换行符转换规则。 考虑分三个层面解决: 首先是立即解决方案,通过IDE设置临时关闭警告。但这样只是治标,用户可能真正需要的是根本性解决。 然后是核心方案,配置.gitattributes文件。引用[3][4]都指向这个方法,这是Git官方推荐的最佳实践。需要明确说明不同设置的效果:text=auto让Git智能转换,* text=auto则强制所有文本文件统一。 最后补充进阶场景:当用户需要混合开发环境时,单独设置core.autocrlf反而更灵活。这里要注意区分Windows(true)和Unix(false)的不同配置值。 用户可能是Java开发者,所以需要强调IDEA特有的全局设置路径。同时考虑到用户可能不熟悉Git底层配置,所有命令都给出完整命令行示例和IDE图形界面位置。 突然想到还需要提醒:如果用户之前提交过错误格式的文件,可能需要额外说明如何清理Git历史记录。不过这个操作较复杂,放在最后作为可选方案更合适。 对了,还要注意语气不能太技术化。虽然这是开发问题,但用户可能刚接触Git配置,要用"行尾符"代替"CRLF/LF"这类术语,并用⌘/Ctrl+Alt+S这样的具体快捷键说明。 </think> 在 IntelliJ IDEA 中解决 `you are about to commit CRLF line separators to the Git repository` 警告,需配置 Git 的行尾符处理规则。以下是详细步骤: ### 方法 1:通过 `.gitattributes` 文件统一行尾符(推荐) 1. **在项目根目录创建或修改 `.gitattributes` 文件** 添加以下内容: ```gitattributes * text=auto # Git 自动处理行尾符 *.sh eol=lf # 指定脚本类文件使用 LF ``` 2. **全局配置 Git(可选但建议)** 命令行执行: ```bash git config --global core.autocrlf input # macOS/Linux git config --global core.autocrlf true # Windows ``` ### 方法 2:修改 IntelliJ IDEA 设置 1. **打开 IDE 设置** `File` → `Settings` (Windows) / `IntelliJ IDEA` → `Preferences` (macOS) 2. **修改行尾符配置** 路径:`Editor` → `Code Style` - 取消勾选 `Transparent native-to-ascii conversion` - 在 `Line Separator` 中选择当前系统的标准(Windows 选 `CRLF`,Unix/macOS 选 `LF`) ![](https://example.com/path/to/image.png) *(图示:IDEA 行尾符设置位置)* 3. **重新提交文件** 修改后重新 commit,警告将消失[^1]。 ### 方法 3:单次忽略警告(临时方案) - 在 Commit 对话框勾选 **`Ignore non-default line separators`** ![](https://example.com/path/to/commit-dialog.png) *(图示:忽略行尾符警告选项)* ### 问题根源说明 此警告是因 **跨系统协作** 导致: - Windows 默认行尾符:`CRLF`(`\r\n`) - Linux/macOS 默认行尾符:`LF`(`\n`) 统一配置后,Git 会自动转换行尾符,避免文件差异混乱[^3][^4]。 > **关键提示**:方法 1 的 `.gitattributes` 是团队协作最佳实践,可彻底解决跨系统行尾符问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值