当前位置: 代码迷 >> Android >> Android 复用自持的控件(2)
  详细解决方案

Android 复用自持的控件(2)

热度:28   发布时间:2016-05-01 14:58:01.0
Android 复用自制的控件(2)
package com.inrayfitness.widget;import com.inrayfitness.R;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class FootView {	private Button back;	private Context context;	private TextView topViewTitle;	public FootView(Context context) {		this.context = context;	}	public FootView init(View view) {		topViewTitle = (TextView) view.findViewById(R.id.topViewTitle);		back = (Button) view.findViewById(R.id.button_back);		back.setOnClickListener(listener);		return this;	}	public FootView setTitle(String title) {		topViewTitle.setText(title);		return this;	}	private OnClickListener listener = new OnClickListener() {		@Override		public void onClick(View v) {			switch (v.getId()) {			case R.id.button_back:				((Activity) context).finish();				break;			default:				break;			}		}	};	private void gotoIntent(Class<?> cls) {		((Activity) context).finish();		Intent intent = new Intent(context, cls);		context.startActivity(intent);	}}
<?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="wrap_content"    android:background="@drawable/noradius_border"    android:orientation="horizontal" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:gravity="center_vertical"        android:orientation="horizontal" >        <Button            android:id="@+id/button_back"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/backimagebutton_style"            android:paddingLeft="20px" />        <TextView            android:id="@+id/topViewTitle"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="3"            android:gravity="center_vertical|center_horizontal"            android:textColor="#FFFFFF"            android:textSize="10pt" />        <Button            android:id="@+id/button_share"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/compimagebutton_style"            android:paddingRight="20px"            android:visibility="gone" />    </LinearLayout></RelativeLayout>

调用方法


在setContentView后 添加

new FootView(context).init(this.findViewById(R.id.header)).setTitle("这里显示标题");/


  相关解决方案