我想在布局的中间画一条线,并使用它作为其他项目的分隔符,如TextView。有没有一个好的小部件。我真的不想使用图像,因为它很难匹配其他组件到它。我也想让它有相对的位置。谢谢
当前回答
使用这段代码。会有帮助的
<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" >
其他回答
你可以在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>
如果你使用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 /可拉的/ 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时才会显示错误。
使用这段代码。会有帮助的
<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" >
使用此XML代码添加垂直线
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:background="#000000" />
使用此XML代码添加水平线
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
运行时版本:
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);
推荐文章
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。
- 如何以编程方式创建ColorStateList ?