一 四種基本佈局
1.線性佈局 LinearLayout
2.相對佈局 RelativeLayout
3.幀佈局 FrameLayout
4.百分比佈局
二 簡單自定義view 通過xml佈局來寫(一個通用的標題欄)
1.標題欄定義的佈局樣式名爲
title_layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="標題" /> <Button android:id="@+id/more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更多" /> </LinearLayout>
2.自定義的標題欄文件
public class TitleLayout extends LinearLayout { public TitleLayout(Context context) { super(context); } private TitleLayoutSet titlelayoutSet; public TitleLayout(final Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title_layout, this); } public void intiData(final TitleLayoutSet titleLayoutSet) { Button back = (Button) findViewById(R.id.back); Button more = (Button) findViewById(R.id.more); final TextView title = (TextView) findViewById(R.id.title); back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); titleLayoutSet.setBackImage(back); titleLayoutSet.setTitleText(title); titleLayoutSet.setMoreText(more); more.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { titleLayoutSet.setMoreClickListener(); } }); } public interface TitleLayoutSet { void setBackImage(Button back); void setTitleText(TextView tv); void setMoreClickListener(); void setMoreText(Button btn); } }
3,使用
在activity的佈局文件中
<com.lvyequeen.test.day01.myView.TitleLayout android:id="@+id/titleSet" android:layout_width="match_parent" android:layout_height="wrap_content"/>
在activity文件中
private void initView() { TitleLayout layout = (TitleLayout) findViewById(R.id.titleSet); layout.intiData(new TitleLayout.TitleLayoutSet() { @Override public void setBackImage(Button back) { back.setBackgroundResource(R.mipmap.ic_launcher); } @Override public void setTitleText(TextView tv) { tv.setText("我的測試標題呀"); } @Override public void setMoreClickListener() { Toast.makeText(getApplicationContext(), "點擊更多了", Toast.LENGTH_SHORT).show(); } @Override public void setMoreText(Button btn) { btn.setText("加載更多"); } }); }