如何动态地向我的小部件添加X个TextView?

时间:2012-07-09 04:50:38

标签: android dynamic android-linearlayout textview android-appwidget

我进行了搜索和搜索,无论出于何种原因,我似乎无法让它发挥作用。

我目前正在做的是我有一个配置类,其中我基于微调器上的选定项目,想要创建x量的TextViews,我也想设置它们的文本。所以我不能在.xml文件中创建这些TextViews,我必须在运行时创建它们,因为在那之前我不知道我需要多少。 必须将这些TextView添加到窗口小部件,以便在按下配置类“添加窗口小部件”按钮创建窗口小部件时显示它们。我想将textviews添加到我的Widgets垂直线性布局中,该布局包含在relativelayout中的水平线性布局中。

到目前为止,我已经能够创建TextView并设置正确的文本,但无论我试图尝试什么,我都无法弄清楚如何将它们添加到小部件中以便实际显示它们。我已经尝试过膨胀小部件布局,获取id并添加它们,试图愚弄RemoteViews,因为我意识到这是一个小部件,并且它应该如何完成。似乎没有什么能让他们想要添加,即使我只是尝试从代码创建1 TextView。我只是不能让它显示自己和我给它的setText。

我在Android 2.2 btw工作,这是我的第一个Android应用程序/小部件,所以请不要太残酷,因为我在某处肆无忌惮地犯了一个愚蠢的错误......

如果您需要代码示例以帮助我更好,请知道,并且生病后尝试添加它。现在我在无法访问代码的计算机上寻求帮助:)

非常感谢您花时间阅读本文!希望有人能告诉我为了完成这件事我必须做些什么。或者它是否可能:S    /克里斯

编辑:

这是我试图让它工作的事情之一,但我想这不起作用,因为我正在使用一个小部件,我应该使用远程视图,并通过它以某种方式做到这一点?从我读过的东西看,远程视图似乎只适用于xml生成的代码。如果确实如此,我将如何创建,并将textview添加到我的小部件? :S

//the context c is defined as this, elsewhere:
c = WidgetConfig.this;

public void createPopulateTable(RemoteViews rv){

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.widget, null);

LinearLayout vll1 = (LinearLayout)layout.findViewById(R.id.wVertLinearL1);

// Create a TextView for the left column
TextView wLabelTv = new TextView(c);
wLabelTv.setId(200);
wLabelTv.setTextColor(Color.BLACK);
wLabelTv.setLayoutParams(new LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT));
wLabelTv.setText("No Classes Today");
vll1.addView(wLabelTv);
}

2 个答案:

答案 0 :(得分:0)

我最终放弃了动态创建我需要的TextViews数量的想法,而是创建了一个基本的排序布局,在xml中,我可以(通过迭代)设置我想要的文本和格式,只需添加整个xml文件一遍又一遍:)

我需要的每一行,总是包含一个textview和3个imageview。我在新的xml文件中创建了这个框架。在代码中我将它们设置为我想要的,并将行添加到主布局中。我为我需要的每一行做了这个。

答案 1 :(得分:0)

Hi Chris below is a resouce I developed to dynamically generate spinners and edit texts which are added to a linear layout hope it helps. This function

赋予元素唯一的ID,可用于操作 元素属性

 /*Dynamic function*/
    public void dynaGen(LinearLayout tbl, ArrayList<Integer> ida, ArrayList<Integer> idab, ArrayList<Integer> idac,
                          ArrayList<Integer> idad, ArrayList<Integer> idae, ArrayList<Integer> idaf, final double eunica) {
        ArrayList<Integer> uniqid = new ArrayList<>();
        ArrayList<String> products = resocs.getallPRD();

        for (int i = 0; i < 6; ++i) {
            uniqid.add(Resources.generateViewId());
        }


        LayoutInflater inflater = (LayoutInflater) ordContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View cont = inflater.inflate(R.layout.order_pluxo, null);

        View child = ((ViewGroup) cont).getChildAt(0);
        Spinner prduct = (Spinner) child;
        prduct.setId(Math.abs(uniqid.get(0)));
        prduct.setPadding(0, 10, 0, 10);
        prduct.setAdapter(new ArrayAdapter(ordContext, R.layout.spinresoc, products) {
            public View getDropDownView(int v, View view, ViewGroup viewGroup) {
                TextView textView = (TextView) super.getDropDownView(v, view, viewGroup);
                textView.setTextSize((float) eunica + 5.0f);
                return textView;
            }

            public View getView(int v, View view, ViewGroup viewGroup) {
                TextView textView = (TextView) super.getView(v, view, viewGroup);
                textView.setTextSize((float) eunica + 5.0f);
                return textView;
            }
        });
        prduct.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) ordContext);
        resocs.spinMod(prduct, eunica);

        View childb = ((ViewGroup) cont).getChildAt(1);
        EditText quanty = (EditText) childb;
        quanty.setId(Math.abs(uniqid.get(1)));
        quanty.setHint("Quantity");
        quanty.setTextSize((float) eunica + 5.0f);
        quanty.addTextChangedListener((TextWatcher) ordContext);

        View childc = ((ViewGroup) cont).getChildAt(2);
        EditText price = (EditText) childc;
        price.setId(Math.abs(uniqid.get(2)));
        price.setHint("Price");
        price.setTextSize((float) eunica + 5.0f);

        View childd = ((ViewGroup) cont).getChildAt(3);
        EditText vat = (EditText) childd;
        vat.setId(Math.abs(uniqid.get(3)));
        vat.setHint("VAT");
        vat.setTextSize((float) eunica + 5.0f);

        View childe = ((ViewGroup) cont).getChildAt(4);
        EditText discount = (EditText) childe;
        discount.setId(Math.abs(uniqid.get(4)));
        discount.setHint("Discount");
        discount.setTextSize((float) eunica + 5.0f);
        discount.addTextChangedListener((TextWatcher) ordContext);

        View childf = ((ViewGroup) cont).getChildAt(5);
        EditText total = (EditText) childf;
        total.setId(Math.abs(uniqid.get(5)));
        total.setHint("Total");
        total.setTextSize((float) eunica + 5.0f);


        tbl.addView(cont, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        ida.add(prduct.getId());
        idab.add(quanty.getId());
        idac.add(price.getId());
        idad.add(vat.getId());
        idae.add(discount.getId());
        idaf.add(total.getId());

    }

/*XML resource*/
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:alignContent="flex_start"
    app:alignItems="flex_start"
    app:flexDirection="row"
    app:flexWrap="nowrap">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="40.0dip"
        android:background="@drawable/spintleric"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:popupBackground="@drawable/spinback_resoc"
        app:layout_flexBasisPercent="35.000004%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:hint="@string/ordestringc"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:editable="false"
        android:hint="@string/ordestringd"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:id="@+id/ordedtxtb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:editable="false"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:hint="@string/ordestringe"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtdta_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:textColor="#ff000000"
        android:hint="@string/ordestringf"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edtxtcomb_resoc"
        android:gravity="center"
        android:minWidth="100.0dip"
        android:padding="10.0dip"
        android:editable="false"
        android:textColor="#ff000000"
        android:hint="@string/ordestringg"
        app:layout_flexBasisPercent="20.0%"
        app:layout_flexGrow="1.5" />
</com.google.android.flexbox.FlexboxLayout>