当前位置: 移动技术网 > 移动技术>移动开发>Android > Android TextView 字数过多将其他的View 挤出视图之外解决方案

Android TextView 字数过多将其他的View 挤出视图之外解决方案

2020年07月23日  | 移动技术网移动技术  | 我要评论

国际惯例先上图
1.正常情况
如图所示

**1.1 布局代码**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <ImageView
        android:id="@+id/head"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/nameRoot"
        android:layout_margin="16dp"
        android:src="@mipmap/head"
        android:layout_width="56dp"
        android:layout_height="56dp"/>


    <LinearLayout
        android:id="@+id/nameRoot"
        app:layout_constraintTop_toTopOf="@+id/head"
        app:layout_constraintBottom_toBottomOf="@+id/head"
        app:layout_constraintLeft_toRightOf="@+id/head"
        app:layout_constraintRight_toLeftOf="@+id/role"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_weight="1"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:text="佐助VS鸣人"
            android:textSize="22sp"
            android:textStyle="bold"
            android:textColor="@color/colorPrimary"
            android:layout_height="wrap_content"/>




        <ImageView
            android:id="@+id/fight"
            app:layout_constraintTop_toTopOf="@+id/name"
            app:layout_constraintBottom_toBottomOf="@id/name"
            app:layout_constraintLeft_toRightOf="@id/name"
            android:src="@mipmap/fight"
            android:layout_width="36dp"
            android:layout_height="36dp"/>

    </LinearLayout>





    <TextView
        android:id="@+id/role"
        android:text="火影忍者"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        app:layout_constraintTop_toTopOf="@+id/head"
        app:layout_constraintBottom_toBottomOf="@+id/head"
        app:layout_constraintLeft_toRightOf="@+id/nameRoot"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>

2.名字字数过多时
TextView文字过长图片被挤掉了
由于文字过长导致图片被挤掉了
解决方案:
在这里插入图片描述
在此控件增加父控件限制TextView 的宽度
代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <ImageView
        android:id="@+id/head"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/nameRoot"
        android:layout_margin="16dp"
        android:src="@mipmap/head"
        android:layout_width="56dp"
        android:layout_height="56dp"/>


    <LinearLayout
        android:id="@+id/nameRoot"
        app:layout_constraintTop_toTopOf="@+id/head"
        app:layout_constraintBottom_toBottomOf="@+id/head"
        app:layout_constraintLeft_toRightOf="@+id/head"
        app:layout_constraintRight_toLeftOf="@+id/role"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_weight="1"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">


                <TextView
                    android:id="@+id/name"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:text="宇智波佐助VS漩涡鸣人"
                    android:textSize="22sp"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimary"
                    android:layout_height="wrap_content"/>




                <ImageView
                    android:id="@+id/fight"
                    app:layout_constraintTop_toTopOf="@+id/name"
                    app:layout_constraintBottom_toBottomOf="@id/name"
                    app:layout_constraintLeft_toRightOf="@id/name"
                    android:src="@mipmap/fight"
                    android:layout_width="36dp"
                    android:layout_height="36dp"/>


jie
            </LinearLayout>



            <View
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="0dp"/>
        </LinearLayout>



    </LinearLayout>





    <TextView
        android:id="@+id/role"
        android:text="火影忍者"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        app:layout_constraintTop_toTopOf="@+id/head"
        app:layout_constraintBottom_toBottomOf="@+id/head"
        app:layout_constraintLeft_toRightOf="@+id/nameRoot"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>

解决后效果图如下:
在这里插入图片描述

本文地址:https://blog.csdn.net/nc_kai/article/details/107495258

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网