Android(UI)一级标题和二级标题跟随处理

1.前言

a.二级标题在一级标题只有一行的时候,跟在后面
b.二级标题不能被一级标题挤出界面
c.二级标题在父布局中,水平居中

2.布局代码

当前最关键就是让一级标题的权重占据二级标题之外的,父布局不能是match_parent,只能是wrap_content,不然效果不能实现。

<?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:orientation="vertical">

    <!--用来增加文字-->
    <EditText
        android:id="@+id/contentEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="内容编辑器" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <!--此处最为关键,让第一个内容的权重随父布局,
        所以需要给父布局wrap_content-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="horizontal">
            
            <TextView
                android:id="@+id/contentTitle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="@color/black"
                android:textSize="18sp" />
            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:text="第二处内容"
                android:textColor="@android:color/darker_gray"
                android:textSize="12sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

3.效果图

跟随时
一级标题超过一行
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容