【问题标题】:How to add X number of TextViews to my widget dynamically?如何动态地将 X 个 TextViews 添加到我的小部件中?
【发布时间】:2018-07-13 14:54:47
【问题描述】:

我搜索了又搜索,但无论出于何种原因,我似乎都无法让它工作。

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

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

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

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

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

编辑:

这是我试图让它工作的事情之一,但我想这不起作用,因为我正在使用一个小部件,我应该使用远程视图并以某种方式完成它?从我读过的东西来看,远程视图似乎只适用于 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);
}

【问题讨论】:

  • 我已经按照这些思路做了一些事情,但这不是问题 atm。问题是,即使我只创建一个新的文本视图,也要将其添加到小部件中,以便实际显示。稍后,我将在上面的问题中添加我尝试过的事情之一的示例。感谢您尝试提供帮助! :) 非常感谢

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


【解决方案1】:

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

我需要的每一行总是包含一个文本视图和 3 个图像视图。我在新的 xml 文件中制作了那个骨架。在代码中,我将它们设置为我想要的,并将该行添加到主布局中。我为我需要的每一行都做了这个。

【讨论】:

    【解决方案2】:
    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
    

    为元素提供可用于操作 元素属性

     /*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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多