首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将addView()转换为inflateLayout

将addView()转换为inflateLayout
EN

Stack Overflow用户
提问于 2012-03-03 22:43:42
回答 2查看 2.2K关注 0票数 0

下面是我的代码:

代码语言:javascript
运行
复制
        LinearLayout ll = (LinearLayout) View.inflate(TabAndPlay.this, R.layout.current_play, null);

        TextView tv = new TextView(TabAndPlay.this);
        tv.setText("hallo");
        tv.setBackgroundColor(Color.GRAY);
        ll.addView(tv);

current_play.xml

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/aktuelle_spiele"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

R.id.llPlay (这里的xml就是这样)

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip">

        <LinearLayout
            android:id="@+id/llPlay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

        </LinearLayout>

    </ScrollView>

</LinearLayout>

下面是onOptionsItemSelected()

代码语言:javascript
运行
复制
        case R.id.current_play:
            LinearLayout mainLayout = (LinearLayout) findViewById(R.id.llPlay);
            mainLayout.removeAllViews();
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.current_play, mainLayout, true); 
            return true;

如果我点击我的菜单项,我当前在R.id.llPlays上的所有视图都会被删除!此时将显示来自current_play的按钮。但是"Here my code“中的TextView没有显示..为什么?我的错误在哪里?

EN

Stack Overflow用户

发布于 2012-03-03 23:21:05

你的问题模棱两可,所以我来猜猜答案:

在您的onOptionsItemSelected()中,搜索id为R.id.llPlayLinearLayout,并使用removeAllViews()删除其中的所有子视图,然后扩展R.layout.current_play并对您的布局进行分析,以使LinearLayout (aktuelle_spiele)包含Button以及TextView(您为其设置了文本"Hallo")。问题是,当您膨胀一个布局时,您只膨胀了当时在该布局上的视图。如果您希望布局还包含您在代码中构建TextView,那么您有两个选择:

1直接在current_play.xml布局中添加TextView,并向其添加id,以便稍后可以检索该TextView(使用findViewById(R.id.the_text))并将文本设置为"Hallo":

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/aktuelle_spiele"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    <TextView 
        android:id="@+id/the_text" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />    
</LinearLayout>

编辑:

2然后在你想要的地方动态构建布局,但是保持对那个膨胀视图的引用。因此,如果您像这样创建一个视图:

代码语言:javascript
运行
复制
 LinearLayout ll = (LinearLayout) View.inflate(TabAndPlay.this, R.layout.current_play, null);
        TextView tv = new TextView(TabAndPlay.this);
        tv.setText("hallo");
        tv.setBackgroundColor(Color.GRAY);
        ll.addView(tv);

然后保持对该膨胀视图ll的引用,并在onOptionsItemSelected()回调中将其传递给主布局:

代码语言:javascript
运行
复制
case R.id.current_play:
            LinearLayout mainLayout = (LinearLayout) findViewById(R.id.llPlay);
            mainLayout.removeAllViews();
            mainLayout.add(ll);
            return true;
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9546981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档