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

发布于 2024-12-13 23:06:40 字数 3903 浏览 3 评论 0原文

我正在制作一个自定义 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" />

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

I'm making a custom XML layout to display a cocktail recipe. Here is my 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" />

As you can see all the content displayed is written into the layout itself or is taken from a string resource. My question is: what would I have to do to keep the layout I created below, but be able to fill in the content programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2024-12-20 23:06:40

您必须执行以下操作...

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

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

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

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

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

然后在 onCreate 开头附近我将用对实际视图的引用如下所示:

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);

You would have to do something like this...

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

Use findView to get a reference to a particular view on the activity. Pass it the Resource identifier you gave in the XML (the id property). Cast it to the correct View type, and then set it's properties.

The resource id's are created automatically from the id attribute in XML, and added to the R class inside R.id..

As an example from the application I'm currently writing, I usually define the views I'm going to be modifying as class level fields near the beginning of the class like so:

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

Then near the start of onCreate I will populate them all with the references to the actial views like this:

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);

Then I can just refer to the fields I defined whenever I need to access them like this:

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

In your XML it looks like you may actually need to dynamically create additional text views on the fly based on the recipe. You can do this like so (where llIngredients maps to some LinearLayout in your XML):

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);
旧城烟雨 2024-12-20 23:06:40

不确定我是否跟随你。我猜这个资源代表了一项活动。您可以通过说 - TextView glassTypeTxtView = findViewById(R.id.glass_type) 来获取对文本视图的引用,例如 glass_type

,并通过说 动态设置其内容>glassTypeTxtView.setText(“新文本”)

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

Not sure I follow you. I'm guessing this resource represents an activity. You can get a reference to your text view, lets say glass_type by saying -

TextView glassTypeTxtView = findViewById(R.id.glass_type) and set its content dynamically by saying glassTypeTxtView.setText("new text").

Please correct me if I misunderstood your question completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文