1.首先创建一个setting_view.xml来设置自定义样式
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号;" />
<EditText
android:id="@+id/edit_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码;" />
<EditText
android:id="@+id/edit_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名;" />
<EditText
android:id="@+id/edit_fore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入性别;" />
</LinearLayout>
2.然后创建CombiningControls类继承LinearLayout;
public class CombiningControls extends LinearLayout {
protected EditText editOne;
protected EditText editTwo;
protected EditText editThree;
protected EditText editFore;
private String account;
private String password;
private String name;
private String gender;
public static final String num="http://schemas.android.com/apk/res/com.example.yuxuhao.brush";
public void initview(Context context) {
View inflate = View.inflate(context, R.layout.setting_view, this);
editOne = (EditText) inflate.findViewById(R.id.edit_one);
editTwo = (EditText) inflate.findViewById(R.id.edit_two);
editThree = (EditText) inflate.findViewById(R.id.edit_three);
editFore = (EditText) inflate.findViewById(R.id.edit_fore);
}
public CombiningControls(Context context) {
super(context);
initview(context);
}
public CombiningControls(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initview(context);
account = attrs.getAttributeValue(num,"account");
password = attrs.getAttributeValue(num,"password");
name = attrs.getAttributeValue(num,"name");
gender = attrs.getAttributeValue(num,"gender");
editOne.setText(account);
editTwo.setText(password);
editThree.setText(name);
editFore.setText(gender);
}
public CombiningControls(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initview(context);
}
}
3.然后在res目录下values创建attrs.xml
<resources>
<declare-styleable name="text">
<attr name="account" format="string"/>
<attr name="password" format="string"/>
<attr name="name" format="string"/>
<attr name="gender" format="string"/>
</declare-styleable>
</resources>
4.在主局中引用,注意添加 xmlns:example="http://schemas.android.com/apk/res/com.example.yuxuhao.brush"要与自定义类中保持一致
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:example="http://schemas.android.com/apk/res/com.example.yuxuhao.brush"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:background="@drawable/fade_color"
android:text="Hello World!" />
<com.example.yuxuhao.brush.CombiningControls
android:id="@+id/combin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
5.主函数内设置
public class MainActivity extends AppCompatActivity {
protected CombiningControls combin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
}
private void initView() {
combin = (CombiningControls) findViewById(R.id.combin);
combin.editOne.setText("账号");
combin.editTwo.setText("密码");
combin.editThree.setText("名字");
combin.editFore.setText("男");
}
}
效果如图;
自定义组装控件
最新推荐文章于 2023-03-21 22:32:16 发布