發現include的otherlayout,並沒有在如我們預期的在id/top這個TextView下面,而是忽略了android:layout_below屬性。經過Google發現,很多人遇到類似的問題。
Android include標籤
android中include標籤是爲了便於控件的覆用的一個很好解決方案。
但是也有一些需要注意的地方,下面是本人在項目中碰到過的一個問題,做此記錄,便於以後查看。
include標籤用法。
1.新建一個xml文件,命名 head.xml
head.xml文件內容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/index_linear_foot"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/head_btn_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
2.新建一個佈局文件,命名 main.xml
main.xml文件內容如下:
<?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"
>
<include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" />
</LinearLayout>
注意:上面我們的include標籤中是沒有爲它指定id的。
3.新建一個MainActivity,設置佈局文件爲main.xml;
4.假設我現在需要在代碼中爲head.xml中的RelativeLayout容器設置背景圖片。
代碼如下:
//獲得佈局容器對象
RelativeLayout head = (RelativeLayout)findViewById(R.id.index_linear_foot);
//設置背景圖片
head.setBackgroundResource(R.drawable.head);
這樣就OK了。
5.剛剛說到,我們的include標籤中是沒有爲它指定id的,假設我們現在的main.xml文件佈局容器是RelativeLayout,而我需要把某個控件放在head.xml下面。就需要使用到RelativeLayout佈局容器的特有屬性 android:layout_below="" 屬性。還需要爲include指定id屬性。
那我們的main.xml文件變成如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include android:id="@+id/main_head" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_headb"
/>
</RelativeLayout>
那接下來我們在運行我們的實例,結果發現,代碼在運行到head.setBackgroundResource(R.drawable.head);
這一句的時候拋異常了
java.lang.NullPointerException
原來:如果include指定了id的話,就不能直接把它裏面的控件當成主xml中的控件來直接獲得了,必須先獲得這個xml佈局文件,再通過佈局文件findViewById來獲得其子控件。
代碼如下
View layout = getLayoutInflater().inflate(R.layout.head, null);
RelativeLayout head= (RelativeLayout)layout.findViewById(R.id.index_linear_foot);
//設置背景圖片
head.setBackgroundResource(R.drawable.head);
這樣就可以了。
作者“earon‘s sky”
我們新建一個IncludeXmlTest工程,我們先從佈局文件上下手,我們新建一個真正的real.xml文件,來實現我們的佈局,代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="show" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="" />
- </LinearLayout>
然後在我們需要引入的xml文件中,include這個real.xml就可以了,我這個main.xml代碼如下:
- <?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" >
- <include
- android:id="@+id/include"
- layout="@layout/real"/>
- </LinearLayout>
這個include部分就是引入了real.xml的佈局,之後我們在程序中只需要佈局main.xml文件即可:
- public class IncludeXmlTestActivity extends Activity {
- private TextView mTextView;
- private Button mButton;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- }
- /**
- * 設置view
- * */
- public void initView(){
- mTextView = (TextView) findViewById(R.id.text);
- mButton = (Button)findViewById(R.id.button);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mTextView.setText("hello, i am here");
- }
- });
- }
- }
怎麼樣簡單吧,大家在重複佈局的時候一定要記住使用哦,最後看下運行效果:
運行程序
點擊按鈕,顯示textView的文字