(原创)实现左侧TextView宽度自适应并且可以显示右侧TextView的布局

效果展示

在这里插入图片描述

先来看看上面的效果
左侧的文字宽度是自适应的,但是右侧又有一个TextView
左侧的文字被限制不能把右侧的挤出屏幕外面
所以如果左侧文字超过指定宽度后多余部分就用省略号表示
实际开发中这种情况在一些列表的item中用的比较多
但实际实现的时候会发现
左侧的总是会把右侧的给挤出去
后来用到了ConstraintLayout布局的链条样式来解决这个问题

ConstraintLayout解决办法

因为代码不是很多,我就直接贴出来了

<?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">


  <TextView
    android:id="@+id/title1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="左侧文字较短时:"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


  <TextView
    android:id="@+id/left1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="左侧文字"
    android:padding="4dp"
    android:textColor="#f00"
    app:layout_constraintEnd_toStartOf="@+id/right1"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/title1"
    app:layout_constraintWidth_default="wrap" />

  <TextView
    android:id="@+id/right1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="右侧文字"
    android:background="@drawable/shapetest"
    android:padding="4dp"
    android:textColor="#000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/left1"
    app:layout_constraintTop_toBottomOf="@+id/title1" />


  <TextView
    android:id="@+id/title2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="左侧文字较长时:"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/left1" />


  <TextView
    android:id="@+id/left2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:padding="4dp"
    android:text="左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字"
    android:textColor="#f00"
    app:layout_constraintEnd_toStartOf="@+id/right2"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/title2"
    app:layout_constraintWidth_default="wrap" />

  <TextView
    android:id="@+id/right2"
    android:layout_width="wrap_content"
    android:background="@drawable/shapetest"
    android:padding="4dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="右侧文字"
    android:textColor="#000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/left2"
    app:layout_constraintTop_toBottomOf="@+id/title2" />

</androidx.constraintlayout.widget.ConstraintLayout>

关键代码在于:

app:layout_constraintHorizontal_chainStyle="packed" 使链条上的元素都打包到一起
app:layout_constraintHorizontal_bias="0"  使左侧控件最左侧对齐
app:layout_constraintWidth_default="wrap" 使左侧文字自适应大小并且不超过约束限制,默认是“spread”,会占用所有符合约束的控件

同时要记得:
左右的TextView的start和end约束要写好

LinearLayout解决办法

后来研究了一下,用LinearLayout也能实现了
代码如下:

<?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">


  <TextView
    android:id="@+id/title1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="左侧文字较长时:"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />



  <androidx.appcompat.widget.LinearLayoutCompat
    android:id="@+id/ll1"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/title1"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <androidx.appcompat.widget.LinearLayoutCompat
      android:layout_width="wrap_content"
      android:orientation="horizontal"
      android:layout_height="wrap_content">
      <TextView
        android:id="@+id/left1"
        android:padding="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字左侧文字"
        android:textColor="#f00"
     />

      <TextView
        android:id="@+id/right1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右侧文字"
        android:textColor="#000"
        android:padding="4dp"
        android:background="@drawable/shapetest"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/left1"
        app:layout_constraintTop_toBottomOf="@+id/title1" />

    </androidx.appcompat.widget.LinearLayoutCompat>


  </androidx.appcompat.widget.LinearLayoutCompat>


</androidx.constraintlayout.widget.ConstraintLayout>

要注意一下:
LinearLayout的外层宽度要固定或者match即可

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要实现安卓TextView自适应字体大小的效果,可以使用以下方法: 1. 使用AutoSizeTextType属性:在XML布局文件中,将TextView的属性设置为`app:autoSizeTextType="uniform"`,这将使TextView的字体大小根据其宽度和高度自动调整。 ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:autoSizeTextType="uniform" /> ``` 2. 使用AutoSizeTextView库:如果您的项目支持Android 8.0(API级别26)以下的版本,可以使用Google提供的AutoSizeTextView库。首先,在项目的build.gradle文件中添加以下依赖项: ```groovy dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:preference-v7:28.0.0' implementation 'com.android.support:autosize:1.1.2' } ``` 接下来,在XML布局文件中使用`AutoSizeTextView`代替`TextView`: ```xml <com.google.android.material.textview.AutoSizeTextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` 这样,AutoSizeTextView会自动根据TextView宽度和高度调整字体大小。 3. 使用代码适配字体大小:您还可以通过编写代码来适应TextView的字体大小。可以使用Paint类中的`setTextSize()`方法来设置字体大小,并根据TextView宽度和高度进行调整。 ```java TextView textView = findViewById(R.id.textView); textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { textView.getViewTreeObserver().removeOnGlobalLayoutListener(this); int width = textView.getWidth(); int height = textView.getHeight(); float fontSize = calculateFontSize(width, height); // 根据宽度和高度计算字体大小 textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize); } }); private float calculateFontSize(int width, int height) { // 根据宽度和高度计算字体大小的逻辑 // 可以根据需求自定义计算方法 } ``` 以上是几种实现安卓TextView自适应字体大小的方法,您可以根据项目需求选择适合您的方法进行使用。希望对您有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值