Android連載1-自定義UI控件

1、 對UI界面盡心自定義組件android

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

      }

    });

  }

}

分別定義了活動與​界面。​git

2、源碼:github

1.項目地址微信

https://github.com/ruigege66/Android/tree/master/UICustomViewsapp

2.CSDN:https://blog.csdn.net/weixin_44630050ide

3.博客園:https://www.cnblogs.com/ruigege0000/學習

4.歡迎關注微信公衆號:傅里葉變換,我的公衆號,僅用於學習交流,後臺回覆」禮包「,獲取大數據學習資料大數據

 

相關文章
相關標籤/搜索