Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建文本视图列表_Java_Android_Arrays_Listview_Textview - Fatal编程技术网

Java 如何创建文本视图列表

Java 如何创建文本视图列表,java,android,arrays,listview,textview,Java,Android,Arrays,Listview,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="v

我想要一个可点击文本视图列表这是我的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>

这可能吗?

如果您不能使用ListView,那么您可以自己动态创建和添加TextView

我假设您有一个字符串数组要显示为TextView:

String[] strings = {"TextA", "TextB"};
LinearLayout layout = (LinearLayout) findViewById(R.id.listLayout);
// you will need to set an id for the layout in the xml
获取将包含所有文本视图的布局:

String[] strings = {"TextA", "TextB"};
LinearLayout layout = (LinearLayout) findViewById(R.id.listLayout);
// you will need to set an id for the layout in the xml
然后遍历列表,为每个字符串创建一个新的TextView,添加onClickListener并对其执行任何操作(如更改文本颜色),然后将其添加到布局中:

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);
}

使用android的ListView小部件,但不能在ListView+中选框,因为在更改文本颜色时遇到困难。为什么不能在列表视图中使用选框?更改文本颜色有什么问题?不确定-似乎尝试了几种方法来将其添加到marquee。你有工作代码吗。