Android自訂布局,android布局
1、首先建立屬性檔案
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="topbar"> <attr name="title" format="string"/> <attr name="titleTextSize" format="dimension" /> <attr name="titleTextColor" format="color" /> <attr name="leftText" format="string"/> <attr name="leftTextBackgroud" format="reference|color" /> <attr name="leftTextColor" format="color" /> <attr name="rightText" format="string"/> <attr name="rightTextBackgroud" format="reference|color" /> <attr name="rightTextColor" format="color" /> </declare-styleable></resources>
2、建立自訂布局類
public class TopBar extends RelativeLayout {//自訂控制項private Button leftButton ,rightButton;private TextView tvTitle;//自訂屬性private int leftTextColor;private Drawable leftBackGround;private String leftText;private int rightTextColor;private Drawable rightBackGround;private String rightText;private String titleText;private float titleTextSize;private int titleTextColor;private topBarListener tListener;public void settListener(topBarListener tListener) {this.tListener = tListener;}public interface topBarListener{public void clickLeft();public void clickRight();}private LayoutParams leftParams,rightParams,titleParams;@SuppressLint("NewApi")public TopBar(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub//擷取屬性值TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.topbar);leftTextColor = ta.getColor(R.styleable.topbar_leftTextColor, 0);leftBackGround = ta.getDrawable(R.styleable.topbar_leftTextBackgroud);leftText = ta.getString(R.styleable.topbar_leftText);rightTextColor = ta.getColor(R.styleable.topbar_rightTextColor, 0);rightBackGround = ta.getDrawable(R.styleable.topbar_rightTextBackgroud);rightText = ta.getString(R.styleable.topbar_rightText);titleText = ta.getString(R.styleable.topbar_title);titleTextSize = ta.getDimension(R.styleable.topbar_titleTextSize, 0);titleTextColor = ta.getColor(R.styleable.topbar_titleTextColor, 0);ta.recycle();//初始化控制項leftButton = new Button(context);rightButton = new Button(context);tvTitle = new TextView(context);leftButton.setText(leftText);leftButton.setBackground(leftBackGround);leftButton.setTextColor(leftTextColor);rightButton.setText(rightText);rightButton.setTextColor(rightTextColor);rightButton.setBackground(rightBackGround);tvTitle.setText(titleText);tvTitle.setTextSize(titleTextSize);tvTitle.setTextColor(titleTextColor);tvTitle.setGravity(Gravity.CENTER);setBackgroundColor(0xfff5346);leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);addView(leftButton, leftParams);rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);addView(rightButton, rightParams);titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);addView(tvTitle, titleParams);leftButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubtListener.clickLeft();}});rightButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubtListener.clickRight();}});}}
3、建立監聽事件並使用
public class MainActivity extends Activity {TopBar top ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);top = (TopBar)findViewById(R.id.myTop);top.settListener(new topBarListener() {@Overridepublic void clickRight() {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "clicked the right Button!", 1000).show();}@Overridepublic void clickLeft() {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "clicked the left Button!", 1000).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.example.topbardemo" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <com.example.topbardemo.TopBar android:id="@+id/myTop" android:layout_width="match_parent" android:layout_height="40dp" custom:leftText="left" custom:leftTextColor="#003300" custom:leftTextBackgroud="#00BFFF" custom:title="title" custom:titleTextColor="#000000" custom:titleTextSize="14sp" custom:rightText="right" custom:rightTextColor="#000000" custom:rightTextBackgroud="#00BFFF"> </com.example.topbardemo.TopBar></RelativeLayout>
幾點注意:1、在使用時需要自訂引用值, xmlns:custom="http://schemas.android.com/apk/res/xxx" xxx是工程包名2、使用完TypedArray需要釋放資源 ta.recycle();
: