【问题标题】:Show a Button seeMore When the text in the text view goes out of view显示按钮 seeMore 当文本视图中的文本消失时
【发布时间】:2012-10-03 15:45:25
【问题描述】:

当文本视图中的文本大于一行或文本视图中的文本超出视图时,我想显示一个按钮 seeMore。我已经使用字符长度和子字符串方法完成了。但问题是当它带有不同的屏幕尺寸时,我无法确定字符串长度。现在我为四种不同的屏幕尺寸使用固定长度,主要是中、普通、大 Xlarge。谁能帮我解决这个问题

多谢不谢……

【问题讨论】:

  • Android 中是否有可用于 seeMore 功能的库?
  • 是的,这就是我正在寻找的...请您发布答案...这对我有很大帮助。请让我知道您如何解决 Android 中不同的屏幕尺寸问题
  • 你需要重写问题。否则我的回答与你的问题无关。
  • 嗨,我已根据需要修改了问题。请看一下。

标签: android android-layout


【解决方案1】:

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

首先,我有一个 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 本身负责不同屏幕尺寸的布局。

【讨论】:

  • 有机会看到截图吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2016-11-20
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多