4

テキスト ビュー内のテキストが 1 行を超える場合、またはテキスト ビュー内のテキストがビューの外に出た場合に、ボタンの seeMore を表示したい.文字長と部分文字列メソッドを使用して実行しました。しかし、問題は、画面サイズが異なる場合、文字列の長さを判断できなかったことです。現在、主に中、通常、大、特大の4つの異なる画面サイズに固定長を使用しています。誰でもこの問題を克服するのを手伝ってくれますか

前もって感謝します....

4

1 に答える 1

0

最近、ユーザーのコメントのリストを表示する必要がある同様の機能を実装しました。各項目には、最大 2 行と「その他」のリンクが表示されます。そのリンクをクリックすると、全文が表示され、「詳細」リンクが非表示になります。

まず、Commentオブジェクトの配列がありました。

public class Comment {
    private boolean showFull;
    private String name;
    private String date,
    private String description;

    //standard constructor and a set of setters and getters, including
    public String getFullDescription();
    public String getShortDescription();
}

ここで、この特定の実装では、短い説明は長い説明の最初の 100 文字に「...」を追加したものです (全体の長さが 100 文字を超える場合)。

これらのオブジェクトの配列をCommentカスタムのデータソースとして使用しましたAdaper:

public class CommentRowAdapter extends BaseAdapter {
    private List<Comment> data = null;
    ...
    //all standard method implementations, including get, count, etc., etc. and then

    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout row = (LinearLayout) (convertView == null ? LayoutInflater
                .from(context).inflate(R.layout.listcomm, parent, false)
                : convertView);
        row.setClickable(false);
        final Comment comment = data.get(position);

        //populate all other elements of the row
                ...

                //and now the description
        if (comment.isShowFull()) {
            TextView tv = (TextView) row.findViewById(R.id.CommentDesc);
            tv.setText(comment.getDescriptionFull());
            tv.setTextColor(context.getResources().getColor(R.color.black));
            tv = (TextView) row.findViewById(R.id.CommentMore);
            tv.setVisibility(View.GONE);            
        } else {
            final TextView tvDesc = (TextView) row.findViewById(R.id.CommentDesc);
            tvDesc.setText(comment.getDescriptionShort());
            tvDesc.setTextColor(context.getResources().getColor(R.color.black));
            final TextView tvMore = (TextView) row.findViewById(R.id.CommentMore);
            tvMore.setVisibility(View.VISIBLE);
            tvMore.setTextColor(context.getResources().getColor(R.color.venue_blue));
            tvMore.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {                    
                    comment.setShowFull(false); 
                    tvDesc.setText(comment.getDescriptionFull());
                    tvMore.setVisibility(View.GONE);
                    tvDesc.invalidate();
                }
            });

        }

        return row;
    }
}

行の XML は

<LinearLayout android:id="@+id/ListPoi"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:padding="5dp"
    android:background="@drawable/listpoi_color" xmlns:android="http://schemas.android.com/apk/res/android">
    />
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView android:id="@+id/CommentName" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="12sp"
            android:gravity="left" android:textStyle="bold" android:text="Name"
            android:singleLine="true" />
        <TextView android:id="@+id/CommentDate" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="12sp"
            android:paddingLeft="5dp" android:textStyle="bold"  android:text="Date" android:singleLine="true" />
    </LinearLayout>

    <TextView android:id="@+id/CommentDesc" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:gravity="bottom"
        android:textSize="12sp" android:text="Description" />

    <TextView android:id="@+id/CommentMore" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:gravity="right"
        android:textSize="12sp" android:text="more" />
</LinearLayout>

さまざまな画面サイズのレイアウトは、リスト自体によって処理されました。

テキストのサイズを文字数で制限するのではなく、テキスト フィールドの高さで制限することで、この実装を拡張できます。 This questionには、テキストビューでテキストのサイズを計算する方法について非常に良い答えがあります. この手法を使用して、切り捨てに使用する必要がある文字数を決定できます。それ以外は、ListViewさまざまな画面サイズでのレイアウトを担当するのはそれ自体です。

于 2012-10-04T11:19:06.390 に答える