自定义组装控件

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("男");
    }
}


效果如图;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值