編輯:關於Android編程
本文實例講述了android使用include調用內部組件的方法。分享給大家供大家參考。具體如下:
例子一:
sublayout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#505050" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SubLayout" /> <Button android:id="@+id/mybutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=" A Button " /> </LinearLayout> mail.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <include android:id="@+id/main1" layout="@layout/sublayout" /> <include android:id="@+id/main2" layout="@layout/sublayout" /> <Button android:id="@+id/startanotheractivity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Start Another Activity " /> </LinearLayout>
如何調用組件include進來的組件呢。
package com.AndroidIncludeLayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidIncludeLayout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View subLayout1 = (View)findViewById(R.id.main1);
View subLayout2 = (View)findViewById(R.id.main2);
Button myButton_main1 = (Button)subLayout1.findViewById(R.id.mybutton);
Button myButton_main2 = (Button)subLayout2.findViewById(R.id.mybutton);
Button startAnotherActivity = (Button)findViewById(R.id.startanotheractivity);
startAnotherActivity.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(AndroidIncludeLayout.this, AnotherActivity.class);
startActivity(intent);
}});
myButton_main1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(AndroidIncludeLayout.this, "Button 1 Pressed", Toast.LENGTH_LONG).show();
}});
myButton_main2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(AndroidIncludeLayout.this, "Button 2 Pressed", Toast.LENGTH_LONG).show();
}});
}
}
但是如果include進來的xml,是
sublayout.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SubLayout" /> <Button android:id="@+id/mybutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=" A Button " /> </merge>
則以上的方法將不能實現,會報空指針。
因為用了merge後,導入進來就相當於是當前view下的組件了,所以直接findViewById就可以了。
這樣的話。。。可以解決了include 多次同一個layout的問題
希望本文所述對大家的Android程序設計有所幫助。
Android Service組件在新進程綁定(bindService)過程
1、首先看兩個例子 (1)進程內 Client端 public class CounterService extends Service impleme
Android系統啟動流程
我們先來看一張圖,它清晰的說明了整個Android系統的啟動流程,參考Android內核開發:圖解Android系統的啟動過程。第一階段:Android設備上電後,首先會
Android實現圖片寬度100%ImageView寬度且高度按比例自動伸縮
在ListView中為了實現圖片寬度100%適應ImageView容器寬度,讓高度自動按比例伸縮功能,查了很多資料,搞了一下午都沒找出個現成的辦法,不過貌似
個人界面 ( 頭像 ) 圖片選擇(相冊,拍照)--如何調用系統的相冊,裁剪並且上傳
##需求:個人界面的,個人頭像圖片的切換方式一:點擊開始切換頭像的pop–相冊選擇二:這裡有兩種方式,從相冊選擇和直接拍照,假設現在是從相冊選擇選擇頭像*我在