android linearlayout 和 textview wrap_content 导致剪切最后一行文本

标签 android android-layout android-linearlayout textview android-constraintlayout

我有一个这样简化的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:text="@string/very_long_text"
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="10dp">

        <Button
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:padding="3dp"
            android:text="cancel"
            android:textSize="24sp"/>

        <Button
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:padding="3dp"
            android:text="yes"
            android:textSize="24sp" />
    </LinearLayout>
</LinearLayout>

@string/very_long_text 的值为:

a very long long long long long long long long long long long long long long long long long long long long long long long long long long long text

结果是:

enter image description here

如您所见,文本被剪切了。我知道我可以将 textviewwidth 更改为 wrap_content 然后文本将完全显示,但它会将布局的宽度更改为完整屏幕,这是不需要的。是什么导致了这个问题?我该如何解决这个问题?

编辑

我用它作为 Activity 的布局,它必须看起来像一个对话框。

最佳答案

Try with below

使用约束布局

<android.support.constraint.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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="a very long long long long long long long long long long long long long long long long long long long long long long long long long long long text"
        android:textSize="24sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:padding="3dp"
        android:text="cancel"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnYes"
        app:layout_constraintTop_toTopOf="@+id/btnYes" />

    <Button
        android:id="@+id/btnYes"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="3dp"
        android:text="yes"
        android:textSize="24sp"
        app:layout_constraintEnd_toStartOf="@+id/btnCancel"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtMessage" />
</android.support.constraint.ConstraintLayout>

输出

enter image description here

关于android linearlayout 和 textview wrap_content 导致剪切最后一行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567053/

相关文章:

android - FragmentPagerAdapter 中的 fragment

android - 保存图像时如何自动给出文件名

java - MPAndroid图表组合图表不显示数据

android - ScrollView 在布局内部不起作用

android - TextView 不显示

java - 单击按钮时显示 PopupWindow?

android - 使用 LinearLayout 的自定义小部件没有得到 onDraw()

android - 使用 Universal Image Loader 在 GridView 中加载图像

android - gradle添加依赖后找不到资源

java - Android JSON 异常 - JSONArray 无法转换为 JSONObject