Android自訂控制項

來源:互聯網
上載者:User

標籤:

先在layout中建立一個自訂控制項的布局檔案(一般為LinearLayout布局) 代碼如下:<?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="50dp"
    android:background="@mipmap/icon_title">

    <Button
        android:id="@+id/btnUser"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@mipmap/icon_body" />

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="首頁"
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnConfig"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@mipmap/icon_left" />

</LinearLayout>   然後建立控制項的類,該類繼承自LInearLayout 代碼如下: package com.example.flypie.notesbook.Layout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.flypie.notesbook.R;

/**
 * Created by FLYPIE on 2015/12/9.
 */
public class TitleLayout extends LinearLayout implements View.OnClickListener{

    Button btnUser;
    Button btnConfig;
    TextView tvTitle;

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.menu_title, this);
        initview();
        setlistener();
    }


    private void setlistener() {
        btnUser.setOnClickListener(this);
        btnConfig.setOnClickListener(this);
    }

    private void initview() {
        btnUser= (Button) findViewById(R.id.btnUser);
        btnConfig= (Button) findViewById(R.id.btnConfig);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnUser:
                Toast.makeText(getContext(), "btnUser", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnConfig:
                Toast.makeText(getContext(),"btnConfig",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
} 最後,只要在需要的地方引入該控制項即可: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.flypie.notesbook.Layout.TitleLayout
        android:id="@+id/zdyTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.example.flypie.notesbook.Layout.TitleLayout>

    <ListView
        android:id="@+id/lvNotes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/zdyTitle">

    </ListView>
</RelativeLayout>

 其中的com.example.flypie.notesbook.Layout.TitleLayout為自訂控制項 

Android自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.