如何以编程方式将内容添加到自定义XML布局?

时间:2011-11-03 19:10:53

标签: android android-layout custom-component

我正在制作自定义XML布局以显示鸡尾酒配方。这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/drinkname" android:layout_height="wrap_content"
    android:textSize="30sp" android:text="@string/drink_name"
    android:layout_width="wrap_content" android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView android:layout_width="fill_parent"
    android:layout_height="2dp" android:background="#FFFFFFFF"
    android:layout_below="@id/drinkname" />

<TextView android:layout_height="wrap_content" android:id="@+id/ingredient_label"
    android:text="Ingredients: " android:layout_width="wrap_content"
    android:layout_below="@+id/drinkname" android:layout_marginLeft="10dp"
    android:layout_marginTop="16dp" />
<TableLayout android:layout_height="wrap_content"
    android:id="@+id/ingredient_table" android:layout_width="wrap_content"
    android:layout_alignTop="@+id/ingredient_label"
    android:layout_alignLeft="@+id/drinkname"
    android:layout_alignParentRight="true">
    <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView android:text="• 2 oz. Gin (Boodles)"android:id="@+id/textView1"
        android:layout_width="wrap_content" 
                android:layout_height="wrap_content" />
    </TableRow>
    <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:text="• 2 oz. Vodka (Stolichnaya)"
            android:id="@+id/textView2" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:text="• 1/2 oz. Vermouth, dry" android:id="@+id/textView3"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </TableRow>
    <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:text="• 3 pieces Olive" android:id="@+id/textView4"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </TableRow>
</TableLayout>

<TextView android:id="@+id/instruction_label"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:layout_below="@id/ingredient_table" android:layout_alignLeft="@id/ingredient_label"
    android:text="Instructions: " android:layout_marginTop="25dp" />
<TextView android:id="@+id/instructions"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:layout_alignTop="@id/instruction_label"
    android:layout_alignLeft="@id/drinkname"
    android:layout_alignParentRight="true" android:text="@string/drink_instructions" />

<TextView android:id="@+id/glass_label" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_below="@id/instructions"
    android:layout_alignLeft="@id/ingredient_label" android:text="Glass:"
    android:layout_marginTop="25dp" />
<TextView android:id="@+id/glass_type"
    android:layout_toRightOf="@id/glass_label" android:text="@string/drink_glass"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:layout_alignTop="@id/glass_label" android:layout_alignLeft="@+id/drinkname" />

如您所见,显示的所有内容都会写入布局本身或从字符串资源中获取。我的问题是:我需要做些什么来保持我在下面创建的布局,但能够以编程方式填写内容?

2 个答案:

答案 0 :(得分:1)

不确定我是否关注你。我猜这个资源代表一项活动。您可以获得对文字视图的引用,让我们说 - {/ 1},然后说 -

glass_type并通过说TextView glassTypeTxtView = findViewById(R.id.glass_type)动态设置其内容。

如果我完全误解了你的问题,请纠正我。

答案 1 :(得分:1)

你必须做这样的事情......

(TextView) this.findViewById(R.id.drinkname).setText("Water");

使用findView获取对活动特定视图的引用。将它传递给您在XML中提供的资源标识符(id属性)。将其转换为正确的视图类型,然后设置它的属性。

资源ID是从XML中的id属性自动创建的,并添加到R内的R.id.类。

作为我正在编写的应用程序的一个例子,我通常将我将要修改的视图定义为类开头附近的类级别字段,如下所示:

TextView tvGameDate;
TextView tvGameTime;
TextView tvHomeTeam;
TextView tvAwayTeam;
TextView tvHomePitcher;

然后在onCreate的开头附近,我将使用对这样的actial视图的引用来填充它们:

tvGameDate = (TextView) this.findViewById(R.id.tvGameDate);
tvGameTime = (TextView) this.findViewById(R.id.tvGameTime);
tvHomeTeam = (TextView) this.findViewById(R.id.tvHomeTeam);
tvAwayTeam = (TextView) this.findViewById(R.id.tvAwayTeam);
tvHomePitcher = (TextView) this.findViewById(R.id.tvHomePitcher);

然后,只要我需要访问它们,我就可以参考我定义的字段:

tvHomeTeam.setText(attributes.getNamedItem("home_long").getNodeValue());
tvHomePitcher.setText(" (" + attributes.getNamedItem("home_pitcher").getNodeValue() + ")");

在您的XML中,您可能实际上需要根据配方动态创建其他文本视图。您可以这样做(其中llIngredients映射到XML中的某些LinearLayout):

TextView tv = new TextView(context);

tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Some text for the new view");
llIngredients.addView(tv);