小编Min*_*Dez的帖子

使用自定义XML属性创建复合控件

我一直在尝试将TextView和EditText组合到一个复合控件中,该控件使用自定义xml元素为每个单独的元素传递默认值.我一直在看这里的教程/文档:
构建复合控件
传递自定义属性

到目前为止我有什么.

Attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FreeText">
        <attr name="label" format="string" />
        <attr name="default" format="string" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

我的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.example.misc.FreeText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        myapp:label="label"
        myapp:default="default"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的复合控制,FreeText:

public class FreeText extends LinearLayout {

    TextView label;
    EditText value;

    public FreeText(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOrientation(HORIZONTAL);

        LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
        lp.weight = 1;

        label = new TextView(context);
        addView(label, lp);

        value = new EditText(context);
        addView(value, lp); …
Run Code Online (Sandbox Code Playgroud)

android android-layout

9
推荐指数
1
解决办法
5496
查看次数

标签 统计

android ×1

android-layout ×1