【问题标题】:How Create more TextView in RelativeLayout in rows android如何在行中的RelativeLayout中创建更多的TextView android
【发布时间】:2018-01-29 05:14:08
【问题描述】:

如何在 relativelayout 中创建更多 TextView

我的代码没有显示 不是那样的:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(5, 10, 5, 10);
    params.setMarginStart(40);
    params.setMarginEnd(40);

    for (int a = 0; a < alquran.getindokata().length; a++) {
        textView = new TextView(context);
        textView.setLayoutParams(params);
        textView.setId(a);
        textView.setGravity(Gravity.CENTER);
        textView.setPadding(10, 5, 10, 5);
        textView.setBackgroundColor(context.getResources().getColor(R.color.hitam_pudar));
        textView.setText(alquran.getarabkata()[a] + "\n" + alquran.getindokata()[a]);
        Log.d(TAG, "VerseID " + id_surat +
                " getKata " + alquran.getindokata()[a]
        );
        holder.terjemahankata.addView(textView);
    }

【问题讨论】:

  • 如果所有 8 个文本视图都在单一布局中,那么这在普通布局中是不可能的。使用 FlowLayout 它是 Git 上可用的 Framelayout 的实现。
  • @ADM 我不明白 FlowLayout 是什么,它是 Framelayout 的实现,请举个例子
  • 我不明白,你想让它滚出屏幕吗?你想要 8 个TextViews 在同一行吗?
  • 如果你喜欢滚动使用 Horizo​​ntalScrollView 并将所有的 textview 放在这个里面
  • @MuhammedRefaat 没有滚动视图

标签: android android-layout android-studio textview


【解决方案1】:

根据您提供的图片,我建议您为此使用非官方的 Google 库 FlexBoxLayout。您有多种选择:

  • 弹性方向
  • flexWrap
  • 证明内容
  • 对齐项目
  • 对齐内容

还有更多!

您可以在FlexBoxLayout 中动态添加元素(在您的情况下为TextView),它会根据您的需要进行排列。

【讨论】:

    【解决方案2】:

    要为单个布局添加更多文本视图,您可以使用库“FlexboxLayout”。

    以下是在您的项目中实现此库的代码:

    将此添加到您的 build.gradle 文件中

    compile 'com.google.android:flexbox:0.2.6'
    

    将此添加到主布局文件

     <com.google.android.flexbox.FlexboxLayout
            android:id="@+id/flexBoxLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:alignContent="flex_start"
            app:alignItems="flex_start"
            app:flexDirection="row"
            app:flexWrap="wrap"
            app:justifyContent="flex_start" />
    

    另一个文本视图文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tvTag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Test"
            android:background="@drawable/rounded_corner_green"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="12sp" />
    </LinearLayout>
    

    Java 代码

    public class MainActivity extends AppCompatActivity {
        private FlexboxLayout flexBoxLayout;
        private List<String> List = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.flex_activity);
    
            flexBoxLayout = (FlexboxLayout) findViewById(R.id.flexBoxLayout);
            List.add("Test Content");
            List.add("Content");
            List.add("Test Content");
            List.add("Test");
            List.add("Test Content");
            List.add("Content");
            List.add("Test Content");
            List.add("Test");
    
    
            setView();
        }
    
        public void setView() {
            flexBoxLayout.removeAllViews();
            for (int i = 0; i < List.size(); i++) {
                final int pos = i;
                final View tagView = this.getLayoutInflater().inflate(R.layout.item_select_intrest_tag, null);
                final TextView tvTag = (TextView) tagView.findViewById(R.id.tvTag);
    
                tvTag.setText(List.get(i));
                tvTag.measure(0, 0);
                tvTag.getMeasuredWidth();
    
    
                tvTag.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "Test Clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                flexBoxLayout.addView(tagView);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 2015-11-02
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      相关资源
      最近更新 更多