Android - 自定义控件(一)
一、直接在xml文件中对控件做处理
A、一个自定义效果的Button
看效果图:

实现方式如下:
<Button
android:id="@+id/bt_select_pic"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="31dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:drawableLeft="@drawable/ic_phone"
android:paddingLeft="9dp"
android:drawablePadding="6dp"
android:gravity="left|center_vertical"
android:text="如需帮助,请拨打:13*-****-1234"
android:textColor="#ef7280"
android:textSize="14dp"
android:background="@drawable/frame_purple" />
drawable目录下的frame_purple.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners android:radius="2.5dp"/>
<stroke android:width="0.5dp" android:color="#eeb2b7" />
</shape>
这个例子,严格来说都不算是自定义控件,仅仅是在xml文件中做了处理,有时候,使用xml文件中的某些属性如drawableLeft、background等,就可以达到自定义控件的效果,再比如:
B、自定义效果的Edittext(效果图如上):
<EditText android:id="@+id/et_name" android:layout_width="173dp" android:layout_height="36dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:singleLine="true" android:textSize="18dp" android:textColorHint="#e1e1e1" android:textColor="#222222" android:background="@drawable/rounded_white_bg" android:hint="@string/doct_name_no_change"/>
drawable目录下的doct_name_no_change.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/white" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" >
</corners>
</shape>
二、一个用户注册时用来展示注册须知的自定义的对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(XXXActivity.this);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.clause_privacy_dialog_layout, null);
// builder.setTitle("注册须知");
builder.setView(textEntryView);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.create().show();
clause_privacy_dialog_layout.xml布局文件的内容:
<?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="vertical" >
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginLeft="7dp"
android:layout_marginRight="11dp"
android:orientation="vertical">
<!-- 版权说明 -->
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="7dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="@string/clause_and_privacy_explain"/>
<!--用户权利和义务 -->
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="7dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="@string/clause_and_privacy_person_rights_obligations"/>
<!-- 公司的权利和义务 -->
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="7dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="@string/clause_and_privacy_ikang_rights_obligations"/>
<!-- 隐私保护 -->
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="7dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="@string/clause_and_privacy_protection"/>
<!-- 解释权 -->
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="7dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="@string/clause_and_privacy_interpretation_power"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
这个也比较简单,只是为对话框指定了一个自定义的内容布局
浙公网安备 33010602011771号