我想在布局的中间画一条线,并将其用作TextView等其他项的分隔符。 是否有一个很好的小部件。 我真的不想使用图像,因为很难将其他组件与它匹配。 我希望它也能相对定位。 谢谢
#1楼
我通常使用此代码添加水平线:
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
要添加垂直分隔符,请切换layout_width和layout_height值
#2楼
我将此添加到我的样式中:
match_parent
1dp
?android:attr/listDivider
然后在我的布局中代码更少,阅读更简单。
#3楼
运行时版本:
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);
#4楼
要完成CamilleSévigny的回答,您还可以定义自己的线形,例如自定义线条颜色。
在drawable目录中定义xml形状。 line_horizontal.xml:
xmlns:app="http://schemas.android.com/apk/res-auto" android:shape="line">
在布局中使用此行以及所需的属性:
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" />
#5楼
使用此代码。 我会帮你的
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" >