Android - 动态地将视图添加到视图中

Jos*_*osh 141 android view dynamic android-layout

我有一个视图布局 -

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="0px"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/items_header"
        style="@style/Home.ListHeader" />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/items_none"
        android:visibility="gone"
        style="@style/TextBlock"
        android:paddingLeft="6px" />

    <ListView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/items_list" />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想要做的是,在我的主要活动中使用这样的布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="0px"
    android:id="@+id/item_wrapper">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想循环遍历我的数据模型,并将包含第一个布局的多个视图注入主布局.我知道我可以通过在代码中完全构建控件来实现这一点,但我想知道是否有一种方法可以动态构建视图,以便我可以继续使用布局而不是将所有内容都放在代码中.

Mar*_*her 218

使用此选项LayoutInflater可基于布局模板创建视图,然后将其注入所需的视图中.

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)

您可能必须调整要插入视图的索引.

另外,根据您希望它如何适应父视图来设置LayoutParams.例如FILL_PARENT,有MATCH_PARENT,等等

  • 任何人都可以告诉**R.id.insert_point**这里是什么? (6认同)
  • 它是您想要将动态生成的布局放置在主视图中的位置。这是您必须从自己的代码中填写的部分。 (2认同)
  • 使用活动中的`LayoutInflater vi = getLayoutInflater();` 来保留活动主题,否则您将无法获得该主题。 (2认同)

Gab*_*gut 35

LayoutInflater课.

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert);
inflater.inflate(R.layout.the_child_view, parent);
Run Code Online (Sandbox Code Playgroud)


Ale*_*dam 19

看起来你真的想要一个带有自定义适配器的ListView来扩展指定的布局.使用ArrayAdapter和方法notifyDataSetChanged()可以完全控制视图生成和渲染.

看看这些教程


Yor*_*ang 11

为了使@Mark Fisher的答案更清晰,插入的视图被夸大应该是布局文件夹下的xml文件,但没有像LinearLayout等内部的布局(ViewGroup).我的例子:

RES /布局/ my_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/i_am_id"
    android:text="my name"
    android:textSize="17sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)

然后,插入点应该是像LinearLayout这样的布局:

RES /布局/ activity_main.xml中

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

    <LinearLayout
        android:id="@+id/insert_point"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

然后代码应该是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping_cart);

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.my_view, null);
    ViewGroup main = (ViewGroup) findViewById(R.id.insert_point);
    main.addView(view, 0);
}
Run Code Online (Sandbox Code Playgroud)

我发布这个非常相似的答案的原因是,当我尝试实现Mark的解决方案时,我陷入了我应该用于insert_point和子视图的xml文件.我首先在子视图中使用了布局,它完全不起作用,这花了我几个小时才弄明白.所以希望我的探索可以节省别人的时间.


小智 10

// Parent layout
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout);

// Layout inflater
LayoutInflater layoutInflater = getLayoutInflater();
View view;

for (int i = 1; i < 101; i++){
    // Add the text layout to the parent layout
    view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);

    // In order to get the view we have to use the new view with text_layout in it
    TextView textView = (TextView)view.findViewById(R.id.text);
    textView.setText("Row " + i);

    // Add the text view to the parent layout
    parentLayout.addView(textView);
}
Run Code Online (Sandbox Code Playgroud)