【问题标题】:combine several types of xml layouts programatically android以编程方式结合几种类型的xml布局android
【发布时间】:2013-03-17 00:14:40
【问题描述】:

我有一个要求,其中有 2 个以编程方式生成的屏幕和 2 个 xml 布局。现在我需要多次组合这些布局。

例如,我有屏幕 1 - 以编程方式创建,屏幕 2 - 以编程方式创建,屏幕 3 - 来自 xml 布局,屏幕 4 - 来自 xml 布局

我的最终布局设计应该是一个单一的屏幕,屏幕 1、屏幕 2、屏幕 3、屏幕 4、屏幕 2……所有屏幕根据我输入的屏幕数量共享相同的屏幕空间。请让我知道方法。一些屏幕具有相对布局和一些线性布局。所以它应该结合这些。

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    您需要在主布局上调用addView()。一旦构建了主布局(包含所有其他布局),addView() 方法将向现有的主布局添加新视图。

    要添加新布局,您需要先对其进行充气。

    LinearLayout primaryLayout;
    
    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    LinearLayout newLayout = (LinearLayout)layoutInflater.inflate(R.layout.your_new_layout, null, false);
    
    primaryLayout.addView(newLayout);
    

    AddView 还提供了一个索引选项,用于将新布局放置在主布局中的特定点。

    尝试从空白的 XML 布局开始(比如称为 primary_layout):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/primaryLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    
    </RelativeLayout>
    

    然后,当您的活动开始时,首先设置它,然后根据需要充气并添加:

    setContentView(R.layout.primary_layout);
    LinearLayout primaryLayout = (LinearLayout) findViewById(R.id.primaryLayout);
    

    然后您可以将新视图添加到该视图中。至于多次添加,我相信是通过引用完成的,所以它只看到一个视图。尝试在方法中构建视图,然后返回视图。如:

    private View buildNewView(){
    
        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );  
        LinearLayout newView = (LinearLayout)layoutInflater.inflate( R.layout.my_new_view null, false );
    
    
        return newView ;
    }
    

    并通过primaryLayout.addView(buildNewView(); 调用它。

    【讨论】:

    • 谢谢你的回复,我照你说的做了。相对布局主要布局 = 新的相对布局(这个); LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);相对布局 newLayout = (RelativeLayout)layoutInflater.inflate(R.layout.layout3, null, false);相对布局 newLayout1 = (RelativeLayout)layoutInflater.inflate(R.layout.layout4, null, false); primaryLayout.addView(newLayout); primaryLayout.addView(newLayout1); setContentView(primaryLayout);但是,只添加了最后一个视图,您能指导一下吗?
    • 另外,我不能多次添加同一个视图,它给出一个错误,指出指定的孩子已经有一个父母,首先调用 removeview
    • @bharath 试试我的答案。它使用 Fragments 来做你想做的事。您甚至可以在 Fragments 中指定 XML 文件或运行时创建的布局。
    【解决方案2】:

    您可以查看 Fragments。他们似乎完全按照您的需要做。以下是它们上 TrainingAPI Guides 的链接。

    在您的 xml 文件中,您可以在 LinearLayout 父级中指定 4 个子布局,每个子布局都有一个属性 android:layout_weight="1",所以每个子布局只会占用相同数量的空间。如果纵向,建议设置android:layout_width="match_parentandroid:layout_height="0dp" 现在,您可以将每个子布局的 id 标记为 id1、id2、id3 等,但您也可以将您将创建的两个布局标记为某物likeandroid:id="@+id/fragment_container_firstandroid:id="@+id/fragment_container_second

    在 Java 代码中,您可以将 contentView 设置为 xml 文件 (setContentView(R.layout.myXMLLayout);) 的 id,按照培训指南链接创建片段的两个实例我在上面提供了这些视图,并使用 getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container_first, firstFragment).commit();getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container_second, secondFragment).commit(); 之类的东西将这些视图添加到您之前在 xml 文件中设置的容器中(如果您使用的是支持库,这是培训指南使用的)。

    我真的希望这对您有所帮助。您可以使用 Fragments 构建一个非常灵活的 UI。例如,稍后,您可以在运行时将前两个片段替换为其他片段,从而增加灵活性。您甚至可以为不同的屏幕尺寸设置不同的 UI,在手机上提供更紧凑的视图,但在平板电脑等更大的屏幕上提供更多功能。

    如果这有帮助,我很乐意听取您的意见你出去!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多