布局中的Android绘图分隔符/分隔线?

And*_*der 684 layout android draw

我想在布局的中间画一条线,并将其用作TextView等其他项的分隔符.是否有一个很好的小部件.我真的不想使用图像,因为很难将其他组件与之匹配.我希望它也能相对定位.谢谢

Ale*_*nko 1643

我通常使用此代码添加水平线:

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>
Run Code Online (Sandbox Code Playgroud)

要添加垂直分隔符,请切换layout_widthlayout_height

  • 或者甚至更好,使用`layout_height ="2dp"和android:background ="?android:attr/listDivider"` (88认同)
  • 您应该使用px而不是dp作为分隔符.除非您确实希望分频器大小发生变化,否则可能会降低到1/2像素以下.:) (16认同)
  • 材料设计规范建议使用1dp厚http://www.google.com/design/spec/components/dividers.html#dividers-specs (9认同)
  • 也适合我.也可以添加android:layout_marginTop ="2dp"(等)来在顶部和底部添加空格. (8认同)
  • 这对于简单的水平线很有用.但是如果你想要在末端褪色,可以在这里使用其他方法. (4认同)
  • 我不得不添加`android:minHeight ="1dp"`来使我的布局中的线条可见.我还没弄清楚原因. (2认同)
  • 简洁优雅的解决方案:) (2认同)
  • 您还可以使用边距属性来获得更有说服力的分隔符.对于水平分隔符`android:layout_marginStart ="5dp"android:layout_marginEnd ="5dp"`对于垂直分隔符`android:layout_marginTop ="5dp"android:layout_marginBottom ="5dp"` (2认同)

tod*_*_fp 590

改进Alex KucherenkoDan Dar3提供的答案

我将此添加到我的样式中:

<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/listDivider</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在我的布局中代码更少,阅读更简单.

<View style="@style/Divider"/>
Run Code Online (Sandbox Code Playgroud)

  • 这很棒,恕我直言最好的解决方案!这样您就不必手动设置颜色,因此当您有多个主题时,一致性会更容易(我使用Theme.Sherlock和Theme.Sherlock.Light). (38认同)
  • 这似乎是最干净的解决方案.谢谢! (3认同)
  • 我认为它也没有在Android Studio预览中显示,但在放大预览后我可以看出显示的模糊线条. (3认同)
  • +1 - 迄今为止我使用的9行<Image>解决方案的绝佳替代品.非常......时尚 (2认同)
  • 这似乎工作,但在Android 21预览与API 21它没有显示...我无法测试这是否只是预览的问题或在真实设备上... (2认同)

Cam*_*gny 135

在你想要分隔符的布局中添加它(修改属性以满足你的需要):

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/divider_horizontal_dark"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" />
Run Code Online (Sandbox Code Playgroud)

  • @Ahmed当你有白色活动背景时我不能使用这个,在这种情况下使用android:src ="@ android:drawable/divider_horizo​​ntal_bright". (4认同)

小智 87

您可以在LinearLayout以下位置使用:

android:divider="?android:dividerHorizontal"
android:showDividers="middle"
Run Code Online (Sandbox Code Playgroud)

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:dividerHorizontal"
    android:showDividers="middle"
    android:orientation="vertical" >            

        <TextView 
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="abcd gttff hthjj ssrt guj"/>

        <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="abcd"/>
        <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="abcd gttff hthjj ssrt guj"/>

        <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="abcd"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这仅适用于API级别11 (4认同)

ppr*_*dos 55

<TextView
    android:id="@+id/line"
    style="?android:attr/listSeparatorTextViewStyle"
    android:paddingTop="5dip"
    android:gravity="center_horizontal"
    android:layout_below="@+id/connect_help"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000" />
Run Code Online (Sandbox Code Playgroud)

  • 然而,这种方法的缺点是,Android不能保证现有的风格. (3认同)

Dee*_*oel 45

使用此代码.我会帮你的

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:divider="?android:dividerHorizontal"
    android:gravity="center"
    android:orientation="vertical"
    android:showDividers="middle" >
Run Code Online (Sandbox Code Playgroud)


Khe*_*raj 42

最简单的方法:

垂直分隔线:

<View style="@style/Divider.Vertical"/>

垂直分隔视图

水平分隔线:

<View style="@style/Divider.Horizontal"/>

水平分隔线视图

这都是肯定的!

把它放进去吧 res>values>styles.xml

<style name="Divider">
    <item name="android:background">?android:attr/listDivider</item> //you can give your color here. that will change all divider color in your app.
</style>

<style name="Divider.Horizontal" parent="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item> // You can change thickness here.

</style>

<style name="Divider.Vertical" parent="Divider">
    <item name="android:layout_width">1dp</item>
    <item name="android:layout_height">match_parent</item>
</style>
Run Code Online (Sandbox Code Playgroud)


and*_*per 17

如果使用actionBarSherlock,则可以使用com.actionbarsherlock.internal.widget.IcsLinearLayout类来支持分隔并在视图之间显示它们.

用法示例:

<com.actionbarsherlock.internal.widget.IcsLinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:divider="@drawable/divider"
    android:dividerPadding="10dp"
    android:orientation="vertical"
    android:showDividers="beginning|middle|end" >
... children...
Run Code Online (Sandbox Code Playgroud)

res/drawable/divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <size android:height="2dip" />

    <solid android:color="#FFff0000" />

</shape>
Run Code Online (Sandbox Code Playgroud)

请注意,由于某种原因,图形设计器中的预览显示"android.graphics.bitmap_delegate.nativeRecycle(I)Z".不知道这意味着什么,但它可以忽略,因为它适用于Android和旧版本的新版本(在Android 4.2和2.3上测试).

似乎只有在为图形设计器使用API​​17时才会显示错误.


Far*_*med 16

写下这个:

 android:divider="?android:dividerHorizontal"
 android:showDividers="middle"
Run Code Online (Sandbox Code Playgroud)

完整的例子:

<LinearLayout
        android:id="@+id/llTipInformation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvServiceRating"
        android:orientation="horizontal"
        android:divider="?android:dividerHorizontal"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:showDividers="middle">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/main.msg.tippercent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/colorWhite"
            android:layout_marginTop="@dimen/activity_vertical_margin"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/main.msg.tiptotal"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/colorWhite"
            android:layout_marginTop="@dimen/activity_vertical_margin"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这应该是可以接受的答案,因为它是向“LinearLayout”添加分隔符的最正确方法 (2认同)

小智 14

<View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:layout_marginTop="4dp"
            android:background="@android:color/darker_gray" />
Run Code Online (Sandbox Code Playgroud)

在两个布局之间放置此代码以获取Divider.


elf*_*ekz 12

添加此视图; 在你的.之间画一个分隔符textviews

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />
Run Code Online (Sandbox Code Playgroud)


小智 11

这是你的答案..这是一个在控件之间画线的例子......

<TextView
            android:id="@+id/textView1"
            style="@style/behindMenuItemLabel1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:text="FaceBook Feeds" />

         <View
             android:layout_width="fill_parent"
             android:layout_height="2dp"
             android:background="#d13033"/>

         <ListView
            android:id="@+id/list1"
            android:layout_width="350dp"
            android:layout_height="50dp" />
Run Code Online (Sandbox Code Playgroud)

此代码在两个控件之间绘制线条......


Wij*_*rma 11

它非常简单.只需创建一个黑色背景颜色的视图.

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000"/>
Run Code Online (Sandbox Code Playgroud)

这将创建一个具有背景颜色的水平线.您还可以像添加任何其他视图一样添加其他属性,如边距,填充等.


小智 10

它为您的布局中的任何位置添加了水平分隔线.

    <TextView
       style="?android:listSeparatorTextViewStyle"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)


Yog*_*iya 8

您可以<View>在First TextView之后使用此元素.

 <View
         android:layout_marginTop="@dimen/d10dp"
         android:id="@+id/view1"
         android:layout_width="fill_parent"
         android:layout_height="1dp"
         android:background="#c0c0c0"/>
Run Code Online (Sandbox Code Playgroud)


alc*_*san 7

运行时版本:

View dividerView = new View(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    ViewGroup.LayoutParams.FILL_PARENT, UIUtils.dpToPix(getContext(), 1));
dividerView.setLayoutParams(lp);

TypedArray array = getContext().getTheme()
    .obtainStyledAttributes(new int[] {android.R.attr.listDivider});
Drawable draw = array.getDrawable(0);       
array.recycle();

dividerView.setBackgroundDrawable(draw);
mParentLayout.addView(dividerView);
Run Code Online (Sandbox Code Playgroud)


小智 7

使用此xml代码添加垂直线

 <View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:background="#000000" />
Run Code Online (Sandbox Code Playgroud)

使用此xml代码添加水平线

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000" />
Run Code Online (Sandbox Code Playgroud)


dre*_*per 6

//for vertical line:

<View
   android:layout_width="1dp"
   android:layout_height="fill_parent"
   android:background="#00000000" />




//for horizontal line: 

<View
   android:layout_width="fill_parent"
   android:layout_height="1dp"
   android:background="#00000000" />
//it works like a charm
Run Code Online (Sandbox Code Playgroud)


Cyc*_*3x3 6

例如,在使用android:layout_weight属性为布局组件分配可用屏幕空间的情况下

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        ...
        ...
    </LinearLayout>

     /* And we want to add a verical separator here */

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        ...
        ...
     </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

要在已占用整个屏幕空间的现有两个布局之间添加分隔符,我们不能只添加另一个LinearLayout,android:weight:"1"因为这将生成我们不想要的三个相等宽度的列.相反,我们将减少我们将为这种新布局提供的空间量.最终代码如下所示:

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        ...
        ...
    </LinearLayout>

                    /* *************** ********************** */

    /* Add another LinearLayout with android:layout_weight="0.01" and 
       android:background="#your_choice" */
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.01"
        android:background="@android:color/darker_gray"
     />

    /* Or View can be used */
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
     />

                     /* *************** ********************** */

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        ...
        ...
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Irs*_*shu 5

如果你要经常使用它,最好的办法是

样式.xml:

<style name="Seperator">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item>
        <item name="android:background">@color/light_color</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

现在在您的布局中,只需添加如下:

<View style="@style/Seperator" />
Run Code Online (Sandbox Code Playgroud)