如何以编程方式访问在xml中声明不可见的视图?

时间:2014-08-05 06:47:08

标签: java android android-view

我有这样的布局,main_layout:

<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">
<RelativeLayout 
            android:id="@+id/testLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            >
            <ImageButton 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="50dp"/>
            <ImageButton 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="100dp"
                android:visibility="invisible"/>
</RelativeLayout>
</RelativeLayout>

现在我想访问java代码中的第二个按钮,因此这是导致NullPointerException的代码部分:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.testLayout);
ImageButton b = (ImageButton) rl.getChildAt(1);

我找到了一个新的有趣点。这段代码显示rl在代码段中只有一个子节点,所以rl根本没有第二个按钮。为什么?

 log.d("child Counter", String.valueOf(rl.getChildCount())); 

这在LogCat中显示1

对于那些认为这是问题原因的人来说,这是onCreate方法,但我知道这不是。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
}

3 个答案:

答案 0 :(得分:2)

步骤1:声明第二个按钮的ID,如下所示,然后访问它。

<RelativeLayout 
            android:id="@+id/testLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            >
            <ImageButton 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="50dp"/>
            <ImageButton 
                android:id="@+id/imgButton2"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="100dp"
                android:visibility="invisible"/>
</RelativeLayout> 

并在java文件中访问它

ImageButton b = (ImageButton)  findViewById(R.id.imgButton2);

步骤2:如果你想使用getChild,请确定你是否能够检索所有的孩子。在你的情况下,相对布局有两个孩子,这可以使用get rl.getChildCount();和getChild来实现并将其分配给视图,如下所示。

for(int i=0;i<r1.getChildCount();i++){
       View child=relativeLayout.getChildAt(i);
       //your processing code....
  }

答案 1 :(得分:2)

你可能想为每个小部件添加一个id

<RelativeLayout 
            android:id="@+id/testLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            >
            <ImageButton 
            android:id="@+id/imageview1"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="50dp"/>
            <ImageButton 
            android:id="@+id/imageview2"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="100dp"
                android:visibility="invisible"/>
</RelativeLayout>

并使用

访问它
ImageButton b = (ImageButton) findViewById(R.id.imageview1);

然后你可以通过

使其可见
b.setVisiblity(View.VISIBLE);

答案 2 :(得分:1)

您的代码运行正常,请尝试这种方式,希望这可以帮助您解决问题。

<强> activity_main.xml中

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="64dp">
    <ImageButton
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="50dp"/>
    <ImageButton
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="100dp"
        android:visibility="invisible"/>
</RelativeLayout>

<强> MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.testLayout);
        ImageButton b1 = (ImageButton) rl.getChildAt(0);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Image Button 1 clicked",Toast.LENGTH_SHORT).show();
            }
        });
        ImageButton b2 = (ImageButton) rl.getChildAt(1);
        b2.setVisibility(View.VISIBLE);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Image Button 2 clicked",Toast.LENGTH_SHORT).show();
            }
        });
    }
}