当文本视图中的文本超出视图时,显示一个“查看更多”按钮。

4
我希望在文本视图中的文本超过一行或文本视图中的文本溢出时显示一个“查看更多”按钮。我已经使用字符长度和子字符串方法完成了这个功能。但问题是,当不同的屏幕尺寸出现时,我无法确定字符串长度。现在我正在为四种不同的屏幕尺寸(中等、普通、大、特大)使用固定长度。有人能帮我解决这个问题吗?
提前感谢您的帮助...

在Android中是否有可用于seeMore功能的库? - Sanal V
是的,这正是我在寻找的...你能否请发布答案...这对我来说将是极大的帮助。请告诉我您如何解决Android中不同屏幕大小问题。 - Sanal V
你需要重新表达问题,否则我的回答将与你的问题毫无关系。 - Aleks G
嗨,我已根据需要修改了问题。请看一下。 - Sanal V
1个回答

0

我最近实现了类似的功能,需要显示用户评论列表。每个项目将显示最多两行和“更多”链接。当单击该链接时,将显示完整文本并隐藏“更多”链接。

首先,我有一个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对象的数组作为自定义Adapter的数据源:

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>

不同屏幕尺寸的布局已经由列表本身处理。

您可以通过不限制文本字符数而是通过文本字段的高度来扩展此实现。这个问题有一个非常好的答案,关于如何计算文本视图中文本的大小。使用该技术,您可以确定需要用于截断的字符数。除此之外,ListView本身负责在不同屏幕尺寸下的布局。


有看到截图的机会吗? - Laurynas G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接