动态向混合的xml/code复合布局中添加视图

3

非常抱歉,如果这与inflate的大量问题/答案重复了,但我无法解决我的问题。

我有一个复合视图(LinearLayout),其中在XML中定义了固定部分,并在代码中添加了其他功能。 我想动态添加视图。

这是XML部分(compound.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/compoundView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView android:id="@+id/myTextView" 
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:text="000"  />
</LinearLayout>

我已经在代码中定义了一个LinearLayout来引用XML文件:

public class CompoundControlClass extends LinearLayout {
    public CompoundControlClass (Context context) {
        super(context);
        LayoutInflater li;
        li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.compound_xml,*ROOT*, *ATTACH*);
    }
    public void addAView(){
        Button dynBut = new Button();
        // buttoin def+layout info stripped for brevity
        addView(dynBut);
    }
}

我尝试使用addAView方法以编程方式添加一个视图。

如果ROOT为null并且ATTACH为false,则根据HierarchyViewer的结果,我有以下层次结构:

  • CompoundControlClass>dynBut

XML中原始的TextView已经不存在了。

如果ROOT为此并且ATTACH为true,则我有以下层次结构:

  • CompoundControlClass>compoundView>myTextView
  • CompoundControlClass>dynBut

我希望有以下层次结构:

  • CompoundControlClass>myTextView
  • CompoundControlClass>dynBut

基本上,代码和XML只是一个唯一的视图。我错过了什么?

按D Yao的反馈回答----------------------

诀窍是将复合组件包含在主布局中,而不是直接引用它。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/comound"
        android:id="@+id/compoundView"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

mainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CompoundControlClass c = (CompoundControlClass) this.findViewById(R.id.compoundView);
        c.addAView(this);
    }  
}

CompoundControlClass.java

public class CompoundControlClass extends LinearLayout {
    public CompoundControlClass(Context context) {
        super(context);
    }
    public CompoundControlClass(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CompoundControlClass(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void addAView(Context context){
         ImageView iv = new ImageView(context);                    
         iv.setImageResource(R.drawable.airhorn);
         addView(iv);
    }
}

compound.xml

<com.sounddisplaymodule.CompoundControlClass    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/compoundView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" > 
    <TextView
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="40sp"
        android:textStyle="bold"
        android:text="0:00"  />     
</com.sounddisplaymodule.CompoundControlClass>
1个回答

1
为什么不直接在LinearLayout上调用addView?根据您列出的需求,我看不出需要基于CompoundControlClass。
LinearLayout v = (LinearLayout)findViewById(R.id.compoundView);
v.addView(dynBut);

在这种情况下,v将包含myTextView,然后是dynBut。
如果您希望添加其他功能并因此真正需要创建复合控件类,则只需将构造函数保留为super(etc)并删除其余部分即可。
然后您的xml将如下所示:
<com.yourpackage.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/compoundView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView android:id="@+id/myTextView" 
        android:layout_width="110dp" 
        android:layout_height="wrap_content"
        android:text="000"  />
</com.yourpackage.CompoundControlClass>

您还需要确保您的CompoundControlClass.java包含适当的构造函数,该构造函数接受Context和属性集。

然后,在您的Java代码中,在调用setContentView之后,您可以执行以下操作:

CompoundControlClass c = (CompoundControlClass)findViewById(R.id.compoundView);
Button b = new Button(context);
//setup b here or inflate your button with inflater
c.addView(b);

这将为您提供所需的层次结构。


和你的XML一样,这里是代码。代码中添加的View是一个Image,但这一部分完全正常工作。 - Breiz
如果您可以发布您的xml以及充气xml的活动的代码,我可以看一下。 - D Yao.
问题在于您正在膨胀activity_main.xml,而该xml中的CompoundControlClass为空。在activity_main.xml中将textview作为子项添加到您的compoundcontrolclass中,这样就可以解决您的问题了。 - D Yao.
同意,但最初的目标是设计一个组件来将布局和行为与外部隔离。如果我这样做,就会回到使用化合物组件的代码而不是 XML。 - Breiz
然后使用include。http://developer.android.com/training/improving-layouts/reusing-layouts.html - D Yao.
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接