0

我已经搜索和搜索,无论出于何种原因,我似乎都无法让它工作。

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

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

我正在使用android 2.2 btw,这是我的第一个android应用程序/小部件,所以请不要太残忍,因为我在某个地方犯了一个愚蠢的错误......

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

非常感谢您花时间阅读本文!希望有人能告诉我我必须做什么才能完成这项工作。或者它是否可能:S /Chris

编辑:

这是我试图让它工作的事情之一,但我想这不起作用,因为我正在使用一个小部件,我应该使用远程视图并以某种方式完成它?从我读过的东西来看,远程视图似乎只适用于 xml 生成的代码。如果情况确实如此,那么我将如何创建文本视图并将其添加到我的小部件?: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);
}
4

2 回答 2

0

I ended up dropping the idea of dynamically creating the amount of TextViews i needed, and instead created a basic layout of sorts, in xml, on which i could (through iterations) set the text and formatting i wanted, and simply add the entire xml file over and over again :)

Each row i needed, was always gonna contain a textview and 3 imageviews. I made that skeleton in the new xml file. And in the code i set them to what i wanted, and added the row to the main layout. I did this for each row i needed.

于 2012-07-11T00:17:19.810 回答
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>
于 2018-07-13T06:54:47.830 回答