添加相同类型的多个视图多个、视图、类型

2023-09-06 01:09:26 作者:囍

所以,我有我做了,基本上显示了两个按钮的一些状态标签这个可爱的小看法。没有什么太复杂了。

So, I have this nice little view that I've made, which basically shows two buttons with some status labels. Nothing too complicated.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">

<LinearLayout android:orientation="horizontal"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:background="@drawable/on_off">
    </ToggleButton>

    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_marginLeft="20px" android:layout_marginRight="20px"
        android:layout_marginTop="3dp" android:layout_marginBottom="3dp">
    </TextView>
    <ImageButton android:src="@drawable/preferences"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:id="@+id/imageButton2" android:background="@android:color/transparent">
    </ImageButton>
</LinearLayout>

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/view_monday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/monday_short"></TextView>
    <TextView android:id="@+id/view_tuesday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/tuesday_short"></TextView>
    <TextView android:id="@+id/view_wednesday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/wednesday_short"></TextView>
    <TextView android:id="@+id/view_thursday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/thursday_short"></TextView>
    <TextView android:id="@+id/view_friday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/friday_short"></TextView>
    <TextView android:id="@+id/view_saturday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/saturday_short"></TextView>
    <TextView android:id="@+id/view_sunday" android:textSize="10dp" android:layout_marginRight="3dp"
        android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#2F4F4F"
        android:text="@string/sunday_short"></TextView>
</LinearLayout>
</LinearLayout>

和我想将它添加到我的主要活动有以下code:

And I want to add it to my main activity with the following code:

LinearLayout root = (LinearLayout)findViewById(R.id.alarms);
View newView = View.inflate(this, R.layout.alarm, null);
alarms.add(newView);

不过,它好像我不能添加一个以上的人,我不知道为什么,或者怎么去解决这个问题可以添加多个副本。此外,我不知道如何访问各个部分,因为他们都具有相同的ID。

However, it seems as if I can't add more than one of them, and I'm not sure why, or how to get around this problem to be able to add multiple copies. Furthermore, I don't know how to access individual parts, as they would all have the same id.

谢谢, 蒂姆·

推荐答案

你是如何想的多个副本添加到根的LinearLayout?

How are you trying to add the multiple copies to the 'root' LinearLayout?

如果你只是想叫 addView(NewView的)两次,然后你想通过两次添加同一个视图对象引用。这是错误的,因为你想添加相同视图对象引用两次。我不能完全肯定的定义的行为是什么,当你做到这一点,但我认为addView()不执行任何操作,第二次,因为它会检查其已持有的引用, NewView的(将不胜感激,如果有人可以证实这是否是正确的或错误的)。

If you're simply trying to call addView(newView) twice, then you're trying to add the same View object reference twice over. This is wrong because you're trying to add the same View object reference twice. I'm not entirely sure what the defined behaviour is when you do this, but I assume that addView() performs no action the second time because it checks that it already holds a reference to newView (would be grateful if anyone could confirm whether that's right or wrong).

所以,你需要夸大两个独立的孩子查看我想情况下,使用比如:

So you need to inflate two separate instances of your child View I think, using say:

View newView0 = View.inflate(this, R.layout.alarm, null);
View newView1 = View.inflate(this, R.layout.alarm, null);

,然后逐一添加。

And then add them individually.

我想,那么你会得到解决重复的ID的问题,通过调用 findViewById()实际子视图,而不是父:

I think you'd then get around the problem of duplicate IDs by calling findViewById() on the actual child Views, as opposed to the parent:

newView0.findViewById( someID )

更新:刚刚测试了code在Eclipse为您服务。我说从你的XML文件中创建了两个孩子浏览到的LinearLayout,然后改中的第二个孩子查看视图中的一个属性(背景颜色为蓝色):

Update: Just tested the code in Eclipse for you. I added two child Views created from your XML file to a LinearLayout, and then changed a property (background colour to blue) of one of the Views within the second child View:

    LinearLayout root = new LinearLayout(this);
    LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.main, null);
    LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.main, null);
    root.addView(newView0);
    root.addView(newView1);
    setContentView(root);
    newView1.findViewById(R.id.view_monday).setBackgroundColor(0xff0000ff);