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