【问题标题】:How to align TextViews one next to each other from beginning to the end?如何从头到尾将TextViews彼此相邻对齐?
【发布时间】:2015-03-25 23:34:06
【问题描述】:

我正在创建一个 android 应用程序,例如,我必须显示 10 个文本视图 - 一个彼此相邻等,不管有多少行,只需自动执行即可。像这张图片中的东西:

我应该使用哪种布局?我尝试了 Gridlayout 和 gridview,但不知何故我无法实现这种行为。有什么想法吗?

谢谢!

【问题讨论】:

  • 第一行显示 3 个按钮和第二行显示 2 的依据是什么?它是固定的还是动态变化的?
  • 是的,动态的 - 基于 textviews 的宽度。
  • 随机对齐吗?比如第一行2个,第二行3个?
  • 没有。它是如何自动上线的。例如,不能有 textview nr。即使有一点空间,第 2 行也有 6 个,因为它不适合那里。无论如何,答案已经找到了:)

标签: android layout alignment


【解决方案1】:

我建议您为此使用单个 texView,除非您希望为每个文本(如文本颜色、背景、字体样式等)提供不同的 UI 设计。
如果您不知道文本的数量,您可以使用单个 textView 并将文本设置为您的串联全文。
您的文本视图应该支持多行,您还可以设置行数的最大限制....
示例代码:

      <TextView 
        android:id="@+id/tvMyText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:singleLine="false" 
        android:maxLines="5"
        android:textColor="@color/black"
        android:textSize="14sp" />

        String finalText = "";
        String[] yourData =  // string your containing all texts
        for ( String text : yourData ) {
            finalText = text + " "; // add space after every text
        }
        TextView tvText = (TextView) view.findViewById( R.id.tvMyText );
        tvText.setText( finalText );   

我不确定这个解决方案是否能完美地满足您的要求……但您可以考虑一下。

【讨论】:

    【解决方案2】:

    您好,这里是使用 custom Library 的示例,其作用类似于 List GitHubLibrary TagLayout

    • 示例代码:-

    mFlowLayout.setAdapter(new TagAdapter<String>(mVals) { @Override public View getView(FlowLayout parent, int position, String s) { TextView tv = (TextView) mInflater.inflate(R.layout.tv, mFlowLayout, false); tv.setText(s); return tv; } });

    -- 这将根据数组中文本的大小动态设置您的文本视图

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2014-05-18
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      相关资源
      最近更新 更多