显示一个按钮seeMore当文本视图中的文本超出视图 [英] Show a Button seeMore When the text in the text view goes out of view

查看:209
本文介绍了显示一个按钮seeMore当文本视图中的文本超出视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个按钮seeMore时,在文本视图中的文本大于一行或者当文本视图中的文本走出view.I都使用字符长度和子方法做了。但问题是,当谈到有不同的屏幕尺寸,我无法确定字符串的长度。现在,我使用的是固定长度为四个不同的屏幕尺寸主要为中,正常大XLARGE。谁能帮我解决这个问题。

I want to show a Button seeMore when the text in the text view is larger than one line or when the text in the text view goes out of the view.I have done it using character length and substring method. But the problem is when it comes with different screen size, i was not able to to determine the string length. Now i am using a fixed length for four different screen sizes mainly medium,Normal ,large Xlarge. Could anyone help me to overcome this issue

由于inadvance ....

Thanks inadvance....

推荐答案

我最近implememented类似的功能,在这里我需要显示的用户的评论列表。每个项目会显示两行max和更多链接。当被点击该链接,全文将被显示,并更多链接隐藏起来。

I have implememented a similar functionality recently, where I needed to show a list of users' comments. Each item would show a max of two lines and "more" link. When that link was clicked, the full text would be shown and "more" link hidden.

首先,我不得不注释对象的数组:

First, I had an array of Comment objects:

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个字符)。

Now, in this particular implementation, the short description was just the first 100 chars of the long description with '...' appended (if the overall length was more than 100 chars).

我用这些注释对象作为数据源的阵列自定义 Adaper

I used the array of these Comment objects as the datasource for a custom 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>

针对不同屏幕尺寸的布局是由列表本身的照顾。

The layout for different screen sizes was taken care of by the list itself.

您可以不受字符数,而是由文本字段的高度限制文字的大小扩展此实现。 <一href=\"http://stackoverflow.com/questions/4794484/calculate-text-size-according-to-width-of-text-area\">This问题有关于如何计算在文本视图文本的大小了很好的答案。使用该技术,你可以决定你需要使用截断的字符数。除此之外,它的的ListView 本身是负责不同屏幕尺寸的布局。

You can extend this implementation by not limiting the size of text by number of characters but rather by the height of the text field. This question has a very good answer about how to compute the size of text in a text view. Using that technique, you can determine the number of characters that you need to use for truncation. Other than that, it's the ListView itself that's responsible for the layout in different screen sizes.

这篇关于显示一个按钮seeMore当文本视图中的文本超出视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆