一、 對UI界面盡心自定義組件 package com.example.uicustomviews; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.v ...
一、 對UI界面盡心自定義組件
package com.example.uicustomviews; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.Window; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } }
<?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:background="@drawable/title_bg" > <!-- 這裡我們自定義了一個背景圖片title_bg,並且在res文件夾中保存了這張圖片 --> <!-- 下麵的margin即代表外邊距,類似於之前學過的HTML/CSS中的屬性設置 --> <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/title_bg" android:text="Back" android:textColor="#fff" /> <TextView android:id="@+id/title_text" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="Title Text" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/title_bg" android:text="Edit" android:textColor="#fff" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.uicustomviews.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content"> </com.example.uicustomviews.TitleLayout> <include layout="@layout/title" /> <!-- 這裡使用include語句來導入我們剛纔寫的標題xml --> </LinearLayout>
package com.example.uicustomviews; import android.widget.LinearLayout; import android.widget.Toast; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.Button; import android.widget.Toast; //import android. public class TitleLayout extends LinearLayout{ public TitleLayout(Context context,AttributeSet attrs) { super(context,attrs); LayoutInflater.from(context).inflate(R.layout.title,this); Button titleBack = (Button) findViewById(R.id.title_back); Button titleEdit = (Button) findViewById(R.id.title_edit); titleBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); titleEdit.setOnClickListener(new OnClickListenter() { @Override public void onClick(View v) { Toast.makeText(getContext(),"You clicked Edit button", Toast.LENGTH_SHORT).show(); } }); } }
分別定義了活動與界面。
二、源碼:
1.項目地址
https://github.com/ruigege66/Android/tree/master/UICustomViews
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關註微信公眾號:傅里葉變換,個人公眾號,僅用於學習交流,後臺回覆”禮包“,獲取大數據學習資料