首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setContentView和findViewById

setContentView和findViewById
EN

Stack Overflow用户
提问于 2015-10-08 05:42:03
回答 6查看 5.1K关注 0票数 9

在阅读了一些相关的文章之后,我已经知道,在尝试通过id将元素找到活动视图层次结构之前,我必须显式地调用setContenView。我创建了一个带有膨胀视图的活动,并且所有的操作都很好。问题是当我试图通过使用下一个xml和代码为该活动设置另一个视图时:

代码语言:javascript
复制
<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"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:background="#B12A6D"
tools:context=".MainActivity"
android:orientation="vertical"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:background="@drawable/bg_purple_500"
    android:layout_margin="0dp"
    >

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/GridLayout1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:columnCount="4"
        android:rowCount="4"
        android:orientation="horizontal"
        tools:context=".GridXMLActivity"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        >

         <ImageView
            android:id="@+id/my_bnt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_new_game"
            android:contentDescription="@string/text"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="10dp"
            />
</GridLayout>

</LinearLayout>

</RelativeLayout>

代码:

代码语言:javascript
复制
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

}


....

protected void method_triggered_from_UI_thread() {

  // Inflate other layout...
  View view = View.inflate(this, R.layout.my_layout, null) ;
  setContentView(view);
  ImageView btn = (ImageView) findViewById(R.id.my_bnt);
  // btn is null...

}

我试过其他一些细微的变化:

代码语言:javascript
复制
View mainView = findViewById(android.R.id.content);
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.my_layout, (ViewGroup) mainView, false)  ;
setContentView(view);
ImageView btn = (ImageView) view.findViewById(R.id.my_bnt);

而且我总是得到btn ==为空。我做错了什么?

Thx

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-10-20 14:15:06

对不起,我没有意识到id my_bnt存在,但只在一个现有的布局(不是在布局-土地或布局-xlarge)。谢谢你的帮助

票数 0
EN

Stack Overflow用户

发布于 2015-10-20 05:31:28

你可以试试LayoutInflater。

代码语言:javascript
复制
View inflatedView = LayoutoutInflater.from(context).inflate(R.layout, null);
setContentView(inflatedView);

快乐编码

票数 2
EN

Stack Overflow用户

发布于 2015-10-17 20:07:18

你得等到替换结束。试试这个:

代码语言:javascript
复制
View view = View.inflate(this, R.layout.my_layout, null);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {          
     @Override          
     public void onGlobalLayout() {              
        // continue the initialization from here:
        btn = (ImageView) view.findViewById(R.id.my_bnt);
        //...

        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);        
     }      
}); 
setContentView(view);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33007484

复制
相关文章

相似问题

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