使用 layout_weight、layout_width 和 maxWidth 的 Android 布局

标签 android android-layout textview android-layout-weight

我正在尝试获取一行文本,类似于

foofoofoo - barbarbar

但如果它不能放在一行中,我希望它省略 foo 和 bar。 即我正在尝试缩短它们。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="0dip"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:ellipsize="true"
        android:text="foofoofoofoo" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:text=" - " />
    <TextView
        android:id="@+id/text_2"
        android:layout_width="wrap_content"                         
        android:layout_height="wrap_content"
        android:maxWidth="0dip"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:ellipsize="true"
        android:text="barbarbarbar" />
</LinearLayout>

请耐心等待这部分工作。

设置TextViewlayout_width成为0diplayout_weight成为1意味着它们将各自占用 50% 的可用空间。

设置singleLinetrueellipsizetrue意味着如果文本大于容器,它们将看起来像foofoo...

因此我的结果(如果文本较长)应该是

foofoo.. - barbar..

这是什么!所以这有效。

现在我要解决的问题是,如果第一个 TextView (id:text_1) 的文本少于 layout_width="0dip" 给出的 50%和 layout_weight="1"我想要它 wrap_content .否则它看起来像:

foo blank space - barbar..

我要

foo - barbarbar

这就是为什么我改变了layout_width="wrap_content"并添加了 android:maxWidth="0dip"但这不起作用!好像不理我说wrap_content并且仍然给这个 View 50%!

我读到您需要添加 android:adjustViewBounds="true"对于 maxWidth工作,但这没有影响。

最佳答案

这是我能做的最好的,尝试更改字符串以查看它是否按预期工作。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,2"
    android:stretchColumns="0,2">
    <TableRow>
        <TextView android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="foofoofoo"/>
        <TextView android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:gravity="center_horizontal"/>
        <TextView android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="barbarbarbarbarbarbarbarbarbarbarbar"/>
    </TableRow>
</TableLayout>

关于使用 layout_weight、layout_width 和 maxWidth 的 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960073/

相关文章:

android - 有没有可能通过 Android Studio 在适用于 Android 的 Windows 子系统上运行 Android 应用程序?

java - Firebase Android 按钮大小问题

android - android中的旋转问题

Android layout_weight 没有按预期工作

Android Studio 无法解析符号textView

android dragshadowbuilder 阴影大小

javascript - Angular 中是否有类似于 Android View Lifecycle 的 onResume 方法?

android - 自动完成编辑文本android

android - 在 TextView 中设置一些文本粗体,但不是所有文本

android - 在 android 中每天首次启动应用程序时显示警报