android - 动态添加 View 到混合 xml/代码复合布局

标签 android android-layout

很抱歉,如果关于 inflate 的大量问题/答案是多余的,但我无法解决我的问题。

我有一个复合 View (LinearLayout),它有一个用 XML 定义的固定部分和代码中的附加功能。我想为其动态添加 View 。

这是 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 以编程方式添加 View 。

如果 ROOT 为空且 ATTACH 为假,我有以下层次结构(每个 HierarchyViewer):

  • 复合控件类>dynBut

XML中原来的TextView没有了。

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

  • CompoundControlClass>compoundView>myTextView
  • 复合控件类>dynBut

我想拥有

  • CompoundControlClass>myTextView
  • 复合控件类>dynBut

代码和 XML 基本上只是一个独特的 View 。 我严重错过了什么?

根据 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);
    }  
}

复合控件类.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);
    }
}

复合.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>

最佳答案

为什么不直接在线性布局上调用 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 包含适当的构造函数,该构造函数同时采用上下文和属性集。

然后,在您的 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);

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

关于android - 动态添加 View 到混合 xml/代码复合布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12078714/

相关文章:

java - LeftNavBar 不支持 android :showAsAction ="always"

android - 使用 LocalBroadcastManager 注册 BroadcastReceiver 时未调用 onReceive

android - 阻止 Android 应用程序启动

android - VideoView-播放swf文件

android - 安卓平板的标准分辨率是多少?

android - 这个 float 菜单是如何实现的?

android - 在 Android 中按下时更改按钮大小的问题

Android:抽屉导航 fragment 内的Viewpager

android - RecyclerView 适配器显示错误的图像

android - 带有动态按钮android的扩展列表中的计时器