繁体   English   中英

以编程方式将多个TextView添加到LinearLayout中

[英]Adding multiple TextViews into LinearLayout programmatically

有长串。 我将长字符串分成单词并为每个单词设置TextView。 (你可以问为什么我需要这个功能?当用户点击textview(word)时,应用程序会显示单词的含义)

...
    TextView tv = new TextView(this);
                tv.setTextSize(24);
                tv.setText(word);
                ll.addView(tv);
...

我的LinearLayout:

<LinearLayout
        android:id="@+id/llReader"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="vertical" >
    </LinearLayout>

如果我将LinearLayout的方向垂直放置,则将每个TextView放在新行中。 例如:

字1

WORD2

WORD3

如果我把它放在水平方向,它只将所有单词放在一行:

字1,字2,字3

在结果word4中,word5,word6不可见。

如何以这种方式以编程方式添加TextViews?

在此输入图像描述

您应该在Vertical LinearLayout中添加Horizo​​ntal LinearLayout

<LinearLayout
    android:id="@+id/verticalLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/horizontalLinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

在你的活动中,

private LinearLayout mReaderLayout;

protected void onCreate(Bundle savedInstanceState) {
    mReaderLayout = (LinearLayout) findViewById(R.layout.llReader);
}

private void addText(String text) {
    TextView textView = new TextView(this);
    textView.setText(text);
    mReaderLayout.addView(textView);
}

您可以使用gridview和适配器。

  <GridView
                    android:id="@+id/gridview1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:columnWidth="50dp"
                    android:gravity="center"
                    android:numColumns="auto_fit"
                    android:stretchMode="columnWidth" >

                </GridView>

并在onCreate

    String[] strings= "Long string".split(" ");
       gridView = (GridView) findViewById(R.id.gridview1);  

              // Create adapter to set value for grid view
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, strings);

                gridView.setAdapter(adapter);

                gridView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                       Toast.makeText(getApplicationContext(),
                        ((TextView) v).getText()  , Toast.LENGTH_SHORT).show();

                    }
                });

您可以立即添加使用RelativeLayout,也可以在线性垂直布局中添加两个按钮,然后将其添加到水平布局中。 然后将下一个垂直线性布局与其他两个按钮一起添加到水平布局中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM