【问题标题】:TextView with ellipsize and buttons带有椭圆和按钮的 TextView
【发布时间】:2011-11-20 10:36:52
【问题描述】:

我正在尝试创建一个带有左右按钮的标题,中间有一个标题,按钮右侧有一个按钮(这将允许选择某些内容)。布局应该是这样的:

|<Button>      <TextView><Button>         <Button>|

如果标题很长,它应该是椭圆形的。为此,textview 似乎需要一个 layout_weight,如 this solution 中所示,否则它会将右侧按钮推离屏幕。但是,为 textview 设置 layout_weight 会将中心按钮推到右侧:

|<Button>      <TextView>         <Button><Button>|

目前的布局是这样的:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <Button
        android:text="Hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:text="Testing"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:singleLine="true"
        android:layout_weight="1"
        android:gravity="center"
        android:ellipsize="end"
        android:inputType="text"
        />

    <Button
        android:text="Hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:text="Hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

[编辑] 尝试 FunkTheMonk 的方法不太奏效,因为如果字符串很长,它最终会将中心按钮推出布局:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <Button
        android:text="Hello1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1"
        >
        <TextView
            android:text="Testing a very long text to see how it works how do you like me now this is still longer yet"
            android:layout_margin="3dp"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:singleLine="true"
            android:gravity="center"
            android:ellipsize="end"
            android:inputType="text"
            />

        <Button
            android:text="Hello2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

    <Button
        android:text="Hello3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

【问题讨论】:

    标签: android alignment tableview


    【解决方案1】:

    我不是 100% 确定你的意思。但我想我明白了。一种简单的方法是使用 tablerow 和 android:layout_marginLeft & right。

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"
                android:layout_gravity="left|top"></Button>
    
            <TextView android:inputType="text" android:gravity="center"
                android:ellipsize="end" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Testing"
                android:singleLine="true"
                android:layout_marginLeft="57dp"></TextView>
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_gravity="center"
                android:text="Hello"
                android:layout_marginRight="57dp"></Button>
    
    
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"
                android:layout_gravity="right|top"></Button>
        </TableRow>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      在它们自己的水平线性布局中添加 TextView 和 Button,并在其上设置权重。将此新线性布局的重心设置为中心,文本视图和按钮将在两个外部按钮之间的可用空间中居中。

      如果标题很长,文本视图将在 eclipsing 之前在线性布局内占用尽可能多的空间。

      【讨论】:

        猜你喜欢
        • 2021-01-19
        • 2018-04-07
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        • 1970-01-01
        • 2012-08-01
        相关资源
        最近更新 更多