【问题标题】:Android: Create custom TextView cardedAndroid:创建自定义 TextView 卡片
【发布时间】:2018-03-16 18:10:39
【问题描述】:

我正在尝试学习自定义视图。为了我创建了一个扩展 CardView 的名为 EditTextCarded 的类。 在init 方法中,我已经初始化了一个 EditText 并为其分配了布局参数。 覆盖 onMeasure 方法并声明视图大小后, 然后通过运行我的customView,屏幕上只出现了卡片视图,而EditText在CardView中不存在或不可见。发生了什么,我的textView在哪里?

class EditTextCarded @JvmOverloads
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int = 0) : CardView(context, attrs, defStyleAttr) {


    init {
        var editText = EditText(context)
        editText.layoutParams = LayoutParams(LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        editText.hint = "Test"
        addView(editText)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        ....
    }
}

我的 XML 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#eee"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.groot.edittext_carded.MainActivity">

    <com.example.groot.edittext_carded.EditTextCarded
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_margin="10dp"/>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    使用 CardView.LayoutParams 将子视图添加到 CardView 中

    public class EditTextCarded extends CardView {
    
    private EditText editText;
    
    public EditTextCarded(Context context) {
        super(context);
        init(context);
    }
    
    public EditTextCarded(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    
    public EditTextCarded(Context context, AttributeSet attrs, int 
    defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    
    private void init(Context context) {
        editText = new EditText(context);
        editText.setHint("EditText");
        CardView.LayoutParams clp = new 
    CardView.LayoutParams(LayoutParams.MATCH_PARENT, 
    LayoutParams.MATCH_PARENT);
        addView(editText,clp);
    }
    }
    

    Xml

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <com.example.groot.edittext_carded.EditTextCarded
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </RelativeLayout>
    

    【讨论】:

    • CardView 没有任何 LayoutParams !!!也许我必须使用 FrameLayout.LayoutParams 而不是 CardView.LayoutParams!
    • 在回答中使用代码。 FrameLayout 的 CardView 子类。 CardView 使用 FrameLayout.LayoutParams
    • 老兄,当我使用 CardView.LayoutParams 时出现此错误:unresolve reference LayoutParams
    • 当我覆盖onMeasure 方法时,textview 不见了!!我应该重写onMeasure 方法吗?或onMeasure 方法仅用于查看
    • 你为什么要测量。你想在 onMeasure 中做什么?检查此链接developer.android.com/reference/android/view/…, int)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多