如何以编程方式将内容添加到自定义 XML 布局?
我正在制作一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须执行以下操作...
使用
findView
获取对活动特定视图的引用。将您在 XML 中提供的资源标识符(id 属性)传递给它。将其转换为正确的视图类型,然后设置其属性。资源 ID 是根据 XML 中的
id
属性自动创建的,并添加到R.id.
内的R
类中。作为我当前正在编写的应用程序的一个示例,我通常将要修改的视图定义为类开头附近的类级别字段,如下所示:
然后在 onCreate 开头附近我将用对实际视图的引用如下所示:
然后,每当我需要访问它们时,我都可以引用我定义的字段,如下所示:
在您的 XML 中,看起来您实际上可能需要根据配方动态创建额外的文本视图。您可以像这样执行此操作(其中 llIngredients 映射到 XML 中的某个 LinearLayout):
You would have to do something like this...
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 theR
class insideR.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:
Then near the start of onCreate I will populate them all with the references to the actial views like this:
Then I can just refer to the fields I defined whenever I need to access them like this:
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 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 sayingglassTypeTxtView.setText("new text")
.Please correct me if I misunderstood your question completely.