【问题标题】:Android layout - align to right without wrapping and reserving spaceAndroid 布局 - 右对齐而不换行和保留空间
【发布时间】:2012-02-25 21:11:55
【问题描述】:

我认为是一个相当简单的布局,结果却不是。

我在线性布局中有几行。

每一行都有一个“标题”、一个“标签”和一个“描述”。

描述出现在标题和标签下方,没有问题。

标题长度不定,应与左上角对齐。标签应该出现在标题的右侧(如果标题很短,则不一定是父级的右边缘)。如果在一行中没有足够的空间容纳两者,则标题应换行并为标签留出空间。

标签有自己的风格和行为,并且必须是独立于标题的元素。

在我有限的经验中,RelativeLayouts 是我最幸运的,所以这是我的第一次尝试:

import android.content.Context;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ExampleRow extends RelativeLayout {

    private TextView title;
    private TextView description;
    private TextView tag;

    public ExampleRow(Context context) {

        super(context);

        LayoutParams lp;        

        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        title = new TextView(context);
        title.setId(1);
        addView(title, lp);                 

        lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, title.getId());
        description = new TextView(context);
        description.setId(2);
        addView(description, lp);       

        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.RIGHT_OF, title.getId());
        lp.addRule(RelativeLayout.ABOVE, description.getId());
        lp.setMargins(20, 0, 0, 0);
        tag = new TextView(context);
        addView(tag, lp);

    }
}

长标题会迫使标签离开屏幕。

然后我尝试了嵌套的 LinearLayouts - 主容器作为具有垂直方向的 LinearLayout,它有一个“顶行”,它是一个包含标题和标签的水平 LinearLayout,底部“行”是描述。这有与上述相同的问题。

接下来我尝试了带有 2 个 TableRows 的 TableLayout,但这不仅失败了,而且还显示了一些奇怪的怪癖,比如根本不渲染某些行,原因我不明白。关于 TableLayout 和 TableRow 的文档似乎有点稀疏,我找不到处理列的 XML 字段的 Java 等效项。

布局需要使用 Java,而不是 XML,原因太长且无趣,无法提及。

感谢任何见解。

TYIA。

/编辑

为了澄清要求,我能想到的最好的例子是 HTML。

<table>
  <tr>
    <td>title</td>
    <td>tag</td>
  </tr>
  <tr>
    <td colspan="2">description...</td>
  </tr>
</table>

标签总是在标题的右边,但只在它需要的右边(取决于标题的宽度)。一旦将标签强制到最右边,标题就会换行。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我认为这是您所追求的布局:

    public class TableLayoutCustom extends TableLayout {
    
        public TableLayoutCustom(Context context) {
            super(context);
    
            this.setColumnShrinkable(0, true);
            this.setColumnStretchable(1, true);
    
            TableRow row1 = new TableRow(context);
    
            TableRow.LayoutParams lp = new TableRow.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            TextView title = new TextView(context);
            title.setText("Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum ");
    
            title.setId(1);
            row1.addView(title, lp);
    
            lp.setMargins(20, 0, 0, 0);
            TextView tag = new TextView(context);
            tag.setText("TAG ME");
            row1.addView(tag, lp);
    
            this.addView(row1);
    
            TableRow row2 = new TableRow(context);
            TableRow.LayoutParams tlp = new TableRow.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            tlp.span = 2;
            TextView description = new TextView(context);
            description.setText("Description here");
            description.setId(2);
            row2.addView(description, tlp);
    
            this.addView(row2);
        }
    
    }
    

    如果您将标题上的文本更改为更短的内容,您会看到tag “跟随” title

    【讨论】:

    • 标签需要在标题右侧,不一定在父级右侧(除非标题太长)。我已经编辑了我的问题以更好地解释要求。
    • 太好了,谢谢。为我找不到的收缩/拉伸/跨度成员 +1
    【解决方案2】:

    经过几次尝试,包括一个包含标题和标签的表格布局的线性布局,以及作为线性布局子级的描述,我将发布我现在使用的内容。

    虽然接近尾声,但我仍然愿意接受更好的答案:

    在标题上使用右边距和在标签上使用等量的负左边距确实实现了基本思想,它假设我知道标签的大小。如果我允许说 100 像素,并且标签文本比这长,它会换行。对于这个特定的设置来说,这并不是什么大问题(因为我有一组可预测的标签),我仍然有兴趣了解“正确”的方法。

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 2014-10-24
      • 2011-12-02
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 2015-04-18
      相关资源
      最近更新 更多