首页编程onfinishinflate?Android自定义layout怎么写

onfinishinflate?Android自定义layout怎么写

编程之家 2023-11-02 200次浏览

朋友们,onfinishinflate和Android自定义layout怎么写是当今热门话题,但是它们的内涵和影响力可能会让人感到困惑。在本篇文章中,我将为你们揭示它们的本质和重要性,希望能够为你们带来新的认识。

onfinishinflate?Android自定义layout怎么写

Android如何自定义LinearLayout

LinearLayout自定义方法有多种:

1、自定义xml布局,然后加载布局,自定义一个View继承LinearLayout

2、在自定义控件中声明它的所有子元素,然后在Layout文件中像使用LinearLayout一样去进行布局。

第二种比较烦,它需要在Layout文件中定义好子元素之后,要在代码 onFinishInflate()进行匹配子元素。

我就说说加载布局文件的方法吧。

首先:定义好layout文件

onfinishinflate?Android自定义layout怎么写

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal">

<ImageView

onfinishinflate?Android自定义layout怎么写

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>

publicclassMyLinearLayoutextendsLinearLayout{

privateImageViewimageView,iv_home,iv_add;

privateTextViewtextView;

publicMyLinearLayout(Contextcontext){

super(context);

//TODOAuto-generatedconstructorstub

}

publicMyLinearLayout(Contextcontext,AttributeSetattrs){

super(context,attrs);

//TODOAuto-generatedconstructorstub

LayoutInflaterinflater=(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);

}

/**

*设置图片资源

*/

publicvoidsetImageResource(intresId){

imageView.setImageResource(resId);

}

/**

*设置显示的文字

*/

publicvoidsetTextViewText(Stringtext){

textView.setText(text);

}

}

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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<spanstyle="font-family:Tahoma,'MicrosoftYahei',Simsun;">"</span>

android:layout_width="wrap_content"

android:background="@drawable/bg"

/>

</LinearLayout>

接下来自定义一个MyLinearLayout继承LinearLayout,并且加载刚刚写好的layout文件。(比如http://www.tiecou.com)

publicclassMyLinearLayoutextendsLinearLayout{

privateImageViewimageView,iv_home,iv_add;

privateTextViewtextView;

publicMyLinearLayout(Contextcontext){

super(context);

//TODOAuto-generatedconstructorstub

}

publicMyLinearLayout(Contextcontext,AttributeSetattrs){

super(context,attrs);

//TODOAuto-generatedconstructorstub

LayoutInflaterinflater=(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);

}

/**

*设置图片资源

*/

publicvoidsetImageResource(intresId){

imageView.setImageResource(resId);

}

/**

*设置显示的文字

*/

publicvoidsetTextViewText(Stringtext){

textView.setText(text);

}

}

最后,要的时候使用定义好的MyLinearLayout控件。

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文件。(比如http://www.tiecou.com)

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控件。

好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!

商业机构网址后缀?商业机密网址后缀名 关键词seo优化排名(如何做关键词排名搜索营销seo优化)