5

次のようなテキストの行を取得しようとしています

foofoofoo-barbarbar

ただし、1行に収まらない場合は、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>

これは部分的に機能します。

TextView'slayout_widthをbe0dipおよびlayout_weighttoに設定する1と、それぞれが使用可能なスペースの50%を占めることになります。

singleLinetotrueellipsizetoを設定すると、テキストがコンテナよりも大きい場合、それらはfoofootrueのように見えます。

したがって、私の結果(テキストが長い場合)は

foofoo..-バーバー..

どっちだ!したがって、これは機能します。

今、私が修正しようとしているのは、最初のTextView(id:text_1)がによって与えられた50%未満のテキストを持っていて、layout_width="0dip"それlayout_weight="1"を望んでいる場合wrap_contentです。それ以外の場合は、次のようになります。

foo 空白スペース -バーバー。

そして私は欲しい

foo-バーバーバー

これが私が変更layout_width="wrap_content"して追加した理由ですandroid:maxWidth="0dip"が、これは機能しません!私が言っていることを無視しwrap_content、それでもこの見解を50%与えているようです!

android:adjustViewBounds="true"動作させるには追加する必要があると読みmaxWidthましたが、これによる影響はありませんでした。

4

2 に答える 2

10

これは私ができる最善の方法です。文字列を変更して、意図したとおりに機能するかどうかを確認してください。

<?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>
于 2011-03-09T18:06:08.637 に答える
2

何が欲しいのかを正確に伝えるのはかなり難しいですが、答えは、コードでレイアウトを動的に調整しない限り、これを行うことはできないと思います。これは、システムに複数の異なる方法で動作するように要求しているためです。

まず、ビュー内のテキストの量が同じである場合に、ビューがスペースを均等に共有するようにします。

次に、他のビューのテキストが少ない場合に、ビューがスペースを均等に共有しないようにします。

ここには、他にも複数のケースがあり、任意の方法で処理できます。しかし、私が伝えようとしているのは、同じレイアウトにさまざまな方法で物事を処理するように依頼しているため、単一のレイアウトではこれを実現できないということです。

于 2011-02-10T17:34:42.040 に答える