【问题标题】:How to add items to an android layout?如何将项目添加到 android 布局?
【发布时间】:2015-11-22 22:42:20
【问题描述】:

在我的主要活动中,我想在布局中添加额外的 TextView 项目。我在MainActivity-createOn 中使用了以下代码(部分基于this postthis answer):

LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
View view = inflater.inflate(R.layout.activity_main, null);
GridLayout item = (GridLayout ) view.findViewById(R.id.main_grid);

final int N = 10; // total number of textviews to add

final TextView[] myTextViews = new TextView[N]; // create an empty array;

for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this);
    // set some properties of rowTextView or something
    rowTextView.setText("This is row #" + i);

    // add the textview to the linearlayout
    item.addView(rowTextView);

    // save a reference to the textview for later
    myTextViews[i] = rowTextView;
}
setContentView(view);

主要活动的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_grid"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            />


        <TextView
            android:text="Email address:"

            android:layout_gravity="right"
            />

        <EditText
            android:ems="10"
            />

        <TextView
            android:text="Password:"

            android:layout_column="0"
            android:layout_gravity="right"
            />

        <EditText
            android:ems="8"
            />

        <Space
            android:layout_row="4"
            android:layout_column="0"
            android:layout_columnSpan="3"
            android:layout_gravity="fill"
            />

        <Button
            android:text="Next"
            android:id="@+id/imageButton1"
            android:layout_row="5"
            android:layout_column="3"
            />
    </GridLayout>

</android.support.design.widget.CoordinatorLayout>

但是,在运行代码时,不会将任何项目添加到主布局中。我没有看到任何其他项目。

最后,我想在网格布局(列和行)中动态添加项目,这些项目可能包含文本和图标,并且是可单击的(即我可以单击此网格视图/布局中的元素/无论如何,开始一个特定的行动。也许这是重要的信息......)

【问题讨论】:

  • 你为什么选择View view = inflater.inflate(R.layout.activity_main, null)而不是setContentView(R.layout.activity_main)?你在屏幕上看到你的布局了吗?
  • 我只是从示例中获取的。 setContentView(R.layout.activity_main) 之前用相同的方法完成...
  • 首先,将getSystemService(Context.LAYOUT_INFLATER_SERVICE) 替换为getLayoutInflater(),这样您就得到了正确的LayoutInflater,配置为考虑您的样式和主题。然后,将您的addView() 调用替换为将GridLayout.LayoutParams 作为第二个参数的调用,指示您希望视图的位置。最后,如果需要,使用 Hierarchy View 或 uiautomatorviewer 查看您的 TextViews 的收盘位置。
  • 不确定我是否可以关注你。在哪里可以找到将GridLayout.LayoutParams 作为第二个参数的调用?您是否建议我检查每个 android 类中的所有可能方法?我真的跟不上……
  • @Alex addView(View) 有一个采用 LayoutParams 的重载选项。

标签: android android-layout


【解决方案1】:

一般来说,如果您想在运行时动态地将项目添加到布局中,您可以执行以下操作(我已将您的 GridLayout 替换为 LinearLayout,因为我之前从未使用过 GridLayout,并且不知道它是否需要特殊布局参数)。

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/main_grid">

        ... existing views...

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

onCreate:

setContentView(R.layout.activity_main);
// `ViewGroup container` is a field
container = (ViewGroup) findViewById(R.id.main_grid); 

setContentView 之后的任何地方:

// `this` would be your activity/application context so it has correct theme info
LayoutInflater inflater = LayoutInflater.from(this); 
TextView[] myTextViews = new TextView[NUMBER_OF_TEXTVIEWS];

for (int i = 0; i < NUMBER_OF_TEXTVIEWS; i++) {
    TextView textView = createNewTextViewWithText("This is row #" + i);

    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    );      

    container.addView(textView, params);

    myTextViews[i] = textView;
}

createNewTextViewWithText(String text):

private TextView createNewTextViewWithText(String text) {
    // I prefer inflating the TextView, as it's easier to
    // specify layout params in XML, IMO
    TextView textView = new TextView(this);
    textView.setText(text);
    return textView;
}

布局参数很重要,因为它告诉 ViewGroup如何添加子视图(TextView)。

【讨论】:

  • 一般来说,我会先尝试在 XML 中处理这个问题 - 这样,您就知道 GridLayout 需要哪些布局参数来实现您想要的效果。
  • 谢谢,但我有一个问题:您的示例代码中的item 定义在哪里?
  • 代码现在似乎可以工作(即应用程序不再崩溃),但除了“MainTitle”之外,我没有看到任何其他内容......
  • 我的“项目”是“容器”。看看你想要完成什么会很有帮助(如果你可以在你的问题中添加一个草图)。您提到它不再崩溃,但崩溃不是您的问题中指出的症状..
  • 这可能是一个很好的近似值:www-10.lotus.com/ldd/pfwiki.nsf/… 我想创建行。每行可能包含一个文本,另一个文本,一个图标(删除一个项目......)类似的东西(我什至想点击它开始一些动作......!!!)也许我的方法完全错误,但是我刚刚开始接触android的表面......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 2012-01-05
相关资源
最近更新 更多