首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带中心文本的垂直线

带中心文本的垂直线
EN

Stack Overflow用户
提问于 2017-05-23 17:30:01
回答 2查看 104关注 0票数 0

我有以下list_item.xml:

代码语言:javascript
复制
<?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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-23 17:44:57

使用这个code..without Framelayout

LinearLayoutweightsum属性结合使用

代码语言:javascript
复制
    <?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中:

代码语言:javascript
复制
    <?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>

输出

票数 2
EN

Stack Overflow用户

发布于 2017-05-23 17:37:26

修改布局如下:

代码语言:javascript
复制
<?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>

您可以使用可绘制的形状作为视图的背景,在中间编辑文本。

代码语言:javascript
复制
<TextView android:text="Text" android:background="@drawable/border"/>

以及可绘图文件夹中的可绘制border.xml

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44141419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档