我有以下list_item.xml:
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/points_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/score_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/zero"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"/>
<LinearLayout
android:id="@+id/points_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/score_b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/zero"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>这一观点的结果是:

但我想要这样的东西:

所以,在垂直边框的顶部只有一段文字,里面有一些文字。
我找到了一条水平线的解决方案,但我无法采用它:Android : horizontal line with text in middle
发布于 2017-05-23 17:44:57
使用这个code..without Framelayout
将LinearLayout与weightsum属性结合使用
<?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="wrap_content"
android:background="@drawable/border"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:id="@+id/points_a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/score_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:weightSum="1">
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="0.20"
android:background="@android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:background="@drawable/border"
android:gravity="center"
android:text="0"
android:textSize="20sp" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="0.20"
android:background="@android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:id="@+id/points_b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/score_b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>在res/drawable/Front.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="#000000" />
<corners android:radius="0dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>输出

发布于 2017-05-23 17:37:26
修改布局如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/points_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/score_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"/>
<LinearLayout
android:id="@+id/points_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/score_b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:textSize="20sp"
android:text="0"
android:background="@android:drawable/btn_default"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>您可以使用可绘制的形状作为视图的背景,在中间编辑文本。
<TextView android:text="Text" android:background="@drawable/border"/>以及可绘图文件夹中的可绘制border.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>https://stackoverflow.com/questions/44141419
复制相似问题