Android:强制TextView自适应内容

3
我有一个表格视图(tableview),在其中手动创建新的行并设置它们的布局。每一行都有三个主要元素: 图像、用于标题的文本视图和用于描述的文本视图。为了将它们移动到我想要的位置,我使用了两个线性布局。我希望描述文本视图(descView)显示两行文字,但实际上只显示了一行。我做错了什么或者应该调用哪个方法,使视图显示两行文字。如果能按单词换行 (而不是字符),那就太棒了。
现在我有一个屏幕截图: enter image description here 这是创建新行的代码('New'是我保存信息(文本、图像等)的对象)。
private TableRow formRow(New item) {

    //prepare table row parameters
    TableRow tr = new TableRow(this);
    tr.setBackgroundResource(R.drawable.results_table_row);
    tr.setVerticalGravity(Gravity.CENTER);
    tr.setTag(item.getTitle());
    tr.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_UP) {
                v.setBackgroundResource(R.drawable.results_table_row);

            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.setBackgroundResource(R.drawable.results_table_row_touched);
            }
            return true;
        }
    });


    //create main layout for content
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.HORIZONTAL);

    //add image
    int imageID = Integer.parseInt(item.getPictureSrc());
    ImageView image = new ImageView(this);
    image.setImageResource(imageID);
    image.setPadding(5, 5, 5, 5);
    layout.addView(image);

    //add another layout for title and description
    LinearLayout descLayout = new LinearLayout(this);
    descLayout.setOrientation(LinearLayout.VERTICAL);
    descLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    //add title
    TextView titleView = new TextView(this);
    titleView.setText(item.getTitle());
    titleView.setPadding(5, 0, 0, 0);
    titleView.setTextAppearance(this, R.style.TableRowStyle_ChildTitle);
    titleView.setLines(1);
    descLayout.addView(titleView);

    //add description
    TextView descView = new TextView(this);
    descView.setText(item.getDescription());
    descView.setPadding(5, 0, 0, 0);
    descView.setTextAppearance(this, R.style.TableRowStyle_ChildText);
    descView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    descView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    //descView.setEllipsize(TextUtils.TruncateAt.END);
    descView.setLines(2);
    descLayout.addView(descView);

    //add description layout to main layout
    layout.addView(descLayout);

    //finally, add layout to row
    tr.addView(layout);

    return tr;
}

如何通过代码实现这个功能?我尝试了descView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT));,但这次它甚至没有显示任何文本。 - Paulius Vindzigelskis
如果我设置实际宽度(>0),则似乎会显示文本并换行,但是宽度是我设置的。如何设置可以获取的宽度? - Paulius Vindzigelskis
我添加了一行代码 int widthForText = GlobalPrefs.getScreenWidth() - MAX_IMAGE_WIDTH - 10; 来计算文本宽度(屏幕宽度 - 图像宽度 - 边距和其他损失)。现在在你的帮助下它可以正常工作。 - Paulius Vindzigelskis
3个回答

1

我有一些应用程序的类似设置,但是个人认为,对于这样的情况,我会放弃2个LinearLayouts并使用单个RelativeLayout,并尝试摆脱:

descView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
descView.setLines(2);

然后我会将项目相对定位,并将TextView的宽度设置为FILL_PARENT,高度设置为WRAP_CONTENT。

使用RelativeLayout可以获得更加统一的外观:

将ImageView设置为ALIGN_PARENT_LEFT,给它左右各一个x-dip的边距。

然后添加您的Title TextView并将其与ImageView的右侧对齐。

接下来添加您的Description TextView并将其与ImageView的右侧对齐,并放置在Title的下方。

由于您是通过代码实现的,因此我会更进一步,因为它们都是相同的字体样式(标题只是加粗),所以将它们合并为单个TextView,然后只需使用:

 textView.setText(Html.fromHtml("<b>" + item.getTitle() + "</b><br>" + item.getDescription())); 

请确保为每个元素赋予唯一的ID以进行链接,并且不要对尚未显示的内容进行对齐,否则会抛出NPE或者显示不正确。

但是,我会优先选择RelativeLayout来列出具有多个组件的项目,而不是LinearLayout。

希望这些信息能够帮助到您~


0
如果您的TableRow只有1列,那么我认为在TableLayout中设置android:shrinkColumns="0"将有助于解决此问题。
将布局参数添加到您的TableRow中:
tr.setLayoutParams(new LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
       TableLayout.LayoutParams.WRAP_CONTENT));

在代码中,我猜想将 setShrinkAllColumns(true) 设置为您的 TableLayout 可能会有所帮助。 - waqaslam
它实际上不起作用,正如我所怀疑的那样,因为行通常显示,但是TextView拒绝显示所有文本。它仅显示一行并在此之后截断所有内容。 - Paulius Vindzigelskis
我认为你的方向错了。我也尝试过那个方法。我还尝试将setLines应用于textview,它会创建这些线,但只填充第一行,其他行则为空白。现在在截图中设置了两行,正如你所看到的,只有一行被填充。 - Paulius Vindzigelskis

0

我将最大行数设置为某个数字,并通过计算到第一个点的字符长度来设置字符长度。现在应用程序知道要显示多少信息,因此不会出现任何错误。


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