Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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 - Fatal编程技术网

Java 如何将属性集动态添加到文本视图?

Java 如何将属性集动态添加到文本视图?,java,android,Java,Android,我想在单击按钮时将textViews添加到表中。下面是我在OnClick Listener中所做的工作: todoTable.addView(new TextView(this)); 我还想向动态创建的TextView添加属性,如文本内容或背景。那我怎么做呢?Android Studio说有这样一个构造函数 new TextView(Context context, AttributeSet attrs) 我可以在AttributeSet参数中包含什么来给TextView一个值,例如“Hel

我想在单击按钮时将
textView
s添加到表中。下面是我在OnClick Listener中所做的工作:

todoTable.addView(new TextView(this));
我还想向动态创建的
TextView
添加属性,如文本内容或背景。那我怎么做呢?Android Studio说有这样一个构造函数

new TextView(Context context, AttributeSet attrs)

我可以在AttributeSet参数中包含什么来给TextView一个值,例如“Hello World!”?

您可以将新的TextView设置为一个变量,然后使用“setText”方法:


有很多方法可以使用,包括“挫折资源”。在这里查看它们:

你可以这样做

 TextView tv = new TextView(getContext());
    tv.setText("text");
    FrameLayout.LayoutParams tP = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    tP.setMargins(1, 2, 3, 4);
    tP.gravity = (Gravity.CENTER | Gravity.TOP);
    tv.setLayoutParams(tP);

这样做,创建一个返回TextView的方法,您也可以设置文本。更简单的方法是只创建文本视图布局,然后重新使用它。AttributeSet主要用于创建自定义视图并需要添加更多属性时

public TextView createTextView(String text) {
    String attributes =
            "<attribute xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
            "android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:padding=\"10dp\" " +
            "android:background=\"@drawable/list_selector_background\" android:clickable=\"true\" " +
            "android:textAppearance = \"@android:style/TextAppearance.Medium\" android:textColor = \"@color/text_colors\" " +
            "android:text=\"" + text +  "\" android:gravity = \"left\" " + "\"/>";

    XmlPullParserFactory factory = null;
    try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new StringReader(attributes));
        parser.next();
        AttributeSet attrs = Xml.asAttributeSet(parser);
        return new TextView(this, attrs);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new TextView(this);
}
public TextView createTextView(字符串文本){
字符串属性=

“我不想声明变量,我希望像那样包含AttributeSet。为什么有人不能正确回答这个问题?Android有一个函数有两个参数,一个是AttributeSet。属性集中可以包含什么?最后,一个不错的答案!您介意解释一下
XMLPullParserFactory
是什么,以及您在
try
catch
es?这似乎不再有效。它会因
ClassCastException而崩溃:android.util.XmlPullAttributes无法强制转换为android.content.res.XmlBlock$Parser
错误。有什么解决方法吗?
public TextView createTextView(String text) {
    String attributes =
            "<attribute xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
            "android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:padding=\"10dp\" " +
            "android:background=\"@drawable/list_selector_background\" android:clickable=\"true\" " +
            "android:textAppearance = \"@android:style/TextAppearance.Medium\" android:textColor = \"@color/text_colors\" " +
            "android:text=\"" + text +  "\" android:gravity = \"left\" " + "\"/>";

    XmlPullParserFactory factory = null;
    try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new StringReader(attributes));
        parser.next();
        AttributeSet attrs = Xml.asAttributeSet(parser);
        return new TextView(this, attrs);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new TextView(this);
}