不少時候,android提供的組件並不能知足咱們的需求,因而咱們不得不按需求開發自定義控件。android
Step 1.app
寫好自定義控件的內部佈局文件。 ide
<?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="@android:color/white" android:orientation="horizontal" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingBottom="5dip" android:paddingLeft="40dip" android:paddingTop="5dip" android:src="@drawable/confirm" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dip" android:text="肯定" android:textColor="#000000" /> </LinearLayout>
Step 2:佈局
寫好自定義的控件類並繼承LinearLayout,並寫好相關方法,用於控制自定義控件的內容。this
package org.hjw.mybutton; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class my extends LinearLayout { private ImageView image; private TextView text; public my(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater ll = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); ll.inflate(R.layout.my_button, this); image = (ImageView) findViewById(R.id.image); text = (TextView) findViewById(R.id.text); } public void setImage(int Resid) { image.setImageResource(Resid); } public void setText(String ext) { text.setText(ext); } }
Step3:spa
須要使用自定義控件時,只須要在xml文件中加入便可,這裏咱們在主佈局文件中加入。code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="horizontal" > <org.hjw.mybutton.my android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/bg_button"
/> <org.hjw.mybutton.my android:id="@+id/myButton2" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:layout_marginLeft="5dp">
</org.hjw.mybutton.my> </LinearLayout>
Step 4:orm
爲了使用戶體驗效果更好,能夠加入背景圖片的變化效果。xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> <item android:state_pressed="true" android:drawable="@drawable/btn_white"></item> <item android:state_checked="true" android:drawable="@drawable/btn_white"></item> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> </selector>
Step 5:blog
最後在主Activity中像使用其餘控件同樣使用此控件。
package org.hjw.mybutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private my My, My2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
My = (my) findViewById(R.id.myButton1);
My2 = (my) findViewById(R.id.myButton2);
My2.setImage(R.drawable.cancel);
My2.setText("取消");
My.setImage(R.drawable.confirm);
My.setText("肯定");
My.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You just clicked!", 1)
.show();
}
});
}
@Override
public 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;
}
}
Step 6:
咱們看看效果吧。
╭︿︿︿╮ {/ o o /} ( (oo) ) ︶ ︶︶