简体   繁体   English

如何创建Textviews列表

[英]How to create a list of Textviews

I'm wanting a list of clickable textviews this is the xml I have 我想要一个可单击的textview列表,这是我拥有的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/marque_scrolling_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="16dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
        android:textSize="24sp" />
</LinearLayout>

Would like a list of items all clickable 想要所有可点击的项目列表

This is the code I'm using: 这是我正在使用的代码:

TextView marque1 = (TextView) this.findViewById(R.id.marque_scrolling_text);
marque1.setSelected(true);

marque1.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v)
    {
        System.out.println("dhgjgf jfgsfjhsgfsjfgdfjh");
    }
});

Is this possible? 这可能吗?

If you can't use ListView then you can dynamically create and add the TextViews yourself! 如果您不能使用ListView,则可以自己动态创建和添加TextViews!

I assume you have an array of Strings you want to show as TextViews: 我假设您有一个要显示为TextViews的String数组:

String[] strings = {"TextA", "TextB"};

Get the Layout that will contain all the TextViews: 获取将包含所有TextViews的布局:

LinearLayout layout = (LinearLayout) findViewById(R.id.listLayout);
// you will need to set an id for the layout in the xml

Then iterate through the list, create a new TextView for each String, add the onClickListener and do whatever you want with it (like changing text colour), and then add it to the Layout: 然后遍历列表,为每个String创建一个新的TextView,添加onClickListener并对其进行任何操作(例如更改文本颜色),然后将其添加到Layout中:

for(String s : strings)
{
     TextView newTextView = new TextView(this);
     newTextView.setText(s);
     newTextView.setTextColor(#0066ff); // for example
     newTextView.setOnClickListener(new View.OnClickListener()  
        { 
            @Override 
            public void onClick(View v)
            { 
              System.out.println("dhgjgf jfgsfjhsgfsjfgdfjh");
            } 
        }); 

     layout.addView(newTextView);
}

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

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