2012-08-03 52 views
0

我想用java語言編寫關於相關佈局的這篇XML代碼。如何使用java語言編寫關於相關佈局的XML代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" android:id="@+id/xK1" 
android:layout_height="wrap_content" android:orientation="vertical" 
android:visibility="gone"> 
<include android:id="@+id/xKeyBoard" layout="@layout/keyboard"></include> 
</RelativeLayout> 

我希望能爲您提供幫助。

回答

1

這是以代碼方式使其以編程方式工作的一種方法。 LayoutInflator正在執行include標籤在XML中的操作。希望這可以幫助!

RelativeLayout rl = new RelativeLayout(this); 
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
rl.setId(someId); 
rl.setVisibilty(View.GONE); 
rl.setOrientation(RelativeLayout.VERTICAL); 

LayoutInflater layoutInflater = (LayoutInflater) 
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
rl.addView(0, layoutInflater.inflate(R.layout.keyboard, this, false));