![](/img/trans.png)
[英]How can I convert an XML to make a programmatically view in Android Java
[英]How can I access to a view programmatically that declare invisible in xml?
我有一個這樣的布局,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);
}
步驟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....
}
您可能想為每個小部件添加一個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);
您的代碼運行正常,請嘗試這種方式,希望這將幫助您解決問題。
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();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.