首页 文章

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

提问于
浏览
685

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

30 回答

  • 549

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

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>
    

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

  • 4

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

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>
    

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

  • 0

    我有最简单的方法来添加分隔符 .

    垂直分隔线:

    <View style =“@ style / dividerVertical”/>

    水平分隔线:

    <View style =“@ style / dividerHorizontal”/>

    这都是肯定的!

    把它放在 res>values>styles.xml

    <style name="dividerBase">
        <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="dividerHorizontal" parent="dividerBase">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item> // You can change thickness here.
    
    </style>
    
    <style name="dividerVertical" parent="dividerBase">
        <item name="android:layout_width">1dp</item>
        <item name="android:layout_height">match_parent</item>
    </style>
    
  • 1532

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

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

    现在在你的布局中,只需添加它:

    <View style="@style/Seperator" />
    
  • 3
    <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" />
    
  • 3

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

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

    <View style="@style/Divider"/>
    
  • 52

    我通常使用这段代码:

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="#aa000000" />
    

    如果您的布局中有一个对象,并且您希望下面的设置行在ImageView中使用此属性:

    android:layout_below="@+id/textBox1"
    
  • 6

    如果使用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...
    

    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>
    

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

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

  • 10

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

    <View
             android:layout_marginTop="@dimen/d10dp"
             android:id="@+id/view1"
             android:layout_width="fill_parent"
             android:layout_height="1dp"
             android:background="#c0c0c0"/>
    
  • 45

    例如,如果您为您的项目使用了recyclerView:

    在build.gradle中写道:

    dependencies {
        compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'
    

    如果要设置颜色,大小和边距值,可以指定如下:

    RecyclerView recyclerView = (RecyclerView) 
    findViewById(R.id.recyclerview);
    recyclerView.addItemDecoration(
            new HorizontalDividerItemDecoration.Builder(this)
                    .color(Color.RED)
                    .sizeResId(R.dimen.divider)
                    .marginResId(R.dimen.leftmargin, R.dimen.rightmargin)
                    .build());
    
  • 6

    use this xml code to add vertical line

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:background="#000000" />
    

    use this xml code to add horizontal line

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />
    
  • 4
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <item
        android:bottom="0dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/divider" />
        </shape>
    </item>
    
  • 5

    添加此视图;在 textviews 之间绘制一个分隔符

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />
    
  • 79
    <View
                android:layout_width="2dp"
                android:layout_height="match_parent"
                android:layout_marginTop="4dp"
                android:background="@android:color/darker_gray" />
    

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

  • 6

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

    <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" />
    
  • 12
    //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
    
  • 13

    简单的解决方案

    只需在您的布局中添加此代码,并将'Id_of__view_present_above'替换为视图的ID,在该视图的下方需要分隔符 .

    <TextView
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="#c0c0c0"
      android:id="@+id/your_id"
      android:layout_marginTop="16dp" 
      android:layout_below="@+id/Id_of__view_present_above"
    />
    
  • 3

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

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

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

  • 11

    例如,在使用 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>
    

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

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

    这是代码“两个文本视图之间的水平分隔线” . 试试这个

    <TextView
            android:id="@id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="5dp"
            android:inputType="textPersonName"
            android:text:"address" />
    
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/black"/>
    
    
        <TextView
            android:id="@id/textView7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName" 
            android:text:"Upload File" />/>
    
  • 1

    这有助于您解决此问题 . 这里创建一个小视图,使黑线成为两个视图之间的分隔符 .

    <View
            android:layout_width="3dp"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
             />
    
  • 1

    使用此代码 . 我会帮你的

    <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" >
    
  • 0

    要完成CamilleSévigny的回答,您还可以定义自己的线条形状,例如自定义线条颜色 .

    在drawable目录中定义xml形状 . line_horizontal.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:shape="line">
        <stroke android:width="2dp" android:color="@android:color/holo_blue_dark" />
        <size android:width="5dp" />
    </shape>
    

    在布局中使用此行以及所需的属性:

    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="2dp"
            android:src="@drawable/line_horizontal" />
    
  • 18

    将空间分成两个相等的部分:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="?android:dividerHorizontal"
            android:showDividers="end"></LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"></LinearLayout>
    
    </LinearLayout>
    

    请注意,一个部分末尾包含一个分隔符

  • 130

    使用以下方法添加水平黑线:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"
        android:layout_marginTop="10dp"/>
    
  • 11
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="2dp"
        android:scaleType="fitXY"
        android:src="?android:attr/listDivider" />
    
  • 15

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

    <TextView
           style="?android:listSeparatorTextViewStyle"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>
    
  • 2

    你可以在 LinearLayout 中使用它:

    android:divider="?android:dividerHorizontal"
    android:showDividers="middle"
    

    例如:

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

    写下这个:

    android:divider="?android:dividerHorizontal"
     android:showDividers="middle"
    

    完整的例子:

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

    运行时版本:

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

相关问题