安卓 c语言画线,布局中的Android绘图分隔符/分隔线?

我想在布局的中间画一条线,并将其用作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_horizo​​ntal.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" >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值