當前位置:編程學習大全網 - 源碼下載 - Android自定義layout怎麽寫

Android自定義layout怎麽寫

LinearLayout自定義方法有多種:

1、自定義xml布局,然後加載布局,自定義壹個View繼承LinearLayout

2、在自定義控件中聲明它的所有子元素,然後在Layout文件中像使用LinearLayout壹樣去進行布局。

第二種比較煩 ,它需要在Layout文件中定義好子元素之後,要在代碼 onFinishInflate() 進行匹配子元素。

我就說說加載布局文件的方法吧。

首先:定義好layout文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:paddingBottom="5dip"

android:paddingLeft="40dip"

android:paddingTop="5dip"

android:src="@drawable/right_icon" />

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:layout_marginLeft="8dip"

android:text="主題"

android:textColor="#000000" />

<LinearLayout

android:layout_width="100dp"

android:layout_height="fill_parent"

android:orientation="horizontal" >

<ImageView

android:id="@+id/imageView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:paddingBottom="5dip"

android:paddingLeft="12dip"

android:paddingTop="5dip"

android:src="@drawable/home_icon" />

<ImageView

android:id="@+id/imageView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:paddingBottom="5dip"

android:paddingLeft="12dip"

android:paddingTop="5dip"

android:src="@drawable/add_icon" />

</LinearLayout>

</LinearLayout>

public class MyLinearLayout extends LinearLayout {

private ImageView imageView,iv_home,iv_add;

private TextView textView;

public MyLinearLayout (Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public MyLinearLayout (Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.actionbar, this);

imageView=(ImageView) findViewById(R.id.imageView1);

iv_home=(ImageView) findViewById(R.id.imageView2);

iv_add=(ImageView) findViewById(R.id.imageView3);

textView=(TextView)findViewById(R.id.textView1);

}

/**

* 設置圖片資源

*/

public void setImageResource(int resId) {

imageView.setImageResource(resId);

}

/**

* 設置顯示的文字

*/

public void setTextViewText(String text) {

textView.setText(text);

}

}

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

<cn.com.demo.view.MyLinearLayout

android:id="@+id/ll_actionbar"

android:layout_height="fill_parent<span style="font-family: Tahoma, 'Microsoft Yahei', Simsun;">" </span>

android:layout_width="wrap_content"

android:background="@drawable/bg"

/>

</LinearLayout>

接下來自定義壹個MyLinearLayout繼承LinearLayout,並且加載剛剛寫好的layout文件。(比如)

public class MyLinearLayout extends LinearLayout {

private ImageView imageView,iv_home,iv_add;

private TextView textView;

public MyLinearLayout (Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public MyLinearLayout (Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.actionbar, this);

imageView=(ImageView) findViewById(R.id.imageView1);

iv_home=(ImageView) findViewById(R.id.imageView2);

iv_add=(ImageView) findViewById(R.id.imageView3);

textView=(TextView)findViewById(R.id.textView1);

}

/**

* 設置圖片資源

*/

public void setImageResource(int resId) {

imageView.setImageResource(resId);

}

/**

* 設置顯示的文字

*/

public void setTextViewText(String text) {

textView.setText(text);

}

}

最後,要的時候使用定義好的MyLinearLayout控件。

  • 上一篇:怎樣在電視盒子上安裝第三方軟件
  • 下一篇:嵌入式是如何做到將某功能植入芯片的
  • copyright 2024編程學習大全網