无法动态添加视图 android

cannot dynamically add views android

我正在尝试使用动态添加的 textview 子项实现自定义视图。但它没有显示任何结果。没有错误,只是没有显示:

我的Class:

public class CustomPasscodeEntryView extends LinearLayout {

private Context mContext;
private CustomPasscodeEntryView thisView;

public CustomPasscodeEntryView(Context context) {
    super(context);
    /* same as below */
}

public CustomPasscodeEntryView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    /*same as below*/
}

public CustomPasscodeEntryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    thisView = (CustomPasscodeEntryView) inflater.inflate(R.layout.view_passcode_entry,this);

}

public void addDigit(int digit){
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final pinView pin = (pinView) inflater.inflate(R.layout.password_bullet_view,null);
    pin.setDigit(digit);
    thisView.addView(pin, thisView.getChildCount());
    pin.setVisibility(VISIBLE);


}
public void deleteDigit(){
    if(getChildCount() > 0)
        thisView.removeView(thisView.getChildAt(getChildCount()-1));
}


}

class pinView extends TextView {

int digit;

public pinView(Context context) {
    super(context);
}

public pinView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public pinView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setText('•');
}

public void setDigit(int digit) {
    this.digit = digit;
    this.setText(Integer.toString(digit));
}
}

和我的 XML: view_passcode_entry:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:animateLayoutChanges="true">
</LinearLayout>

password_bullet_view:

  <com.github.lockpin.lollipin.lib.views.pinView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="@android:color/white"
android:text="•"
android:textSize="20sp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2.5dp"
/>

编辑:我的视图 activity:

    <com.github.lockpin.lollipin.lib.views.CustomPasscodeEntryView
        android:id="@+id/custom_passcode_entry_view"
        android:layout_width="300dp"
        android:layout_below="@id/pin_code_text_view"
        android:layout_centerHorizontal="true"
        android:layout_height="40dp"/>

我看不出有什么问题,我已经按照在线教程进行了一些更改。我在这里错过了什么吗?或者这显然是错误的?

您在扩展视图时使用了错误的视图 ID view_passcode_entry,它是 LinearLayout 的实例,并将其类型转换为 CustomPasscodeEntryView

addDigit()中调用addView()时只需要使用this,因为CustomPasscodeEntryView也是LinearLayout的实例。所以布局 view_passcode_entry 可以删除,因为它不再需要了。

this.addView(pin, this.getChildCount());

但是如果你想将视图 view_passcode_entry 添加为 CustomPasscodeEntryView 实例的子视图,那么你还需要添加 thisView

private ViewGroup thisView;
...
thisView = (ViewGroup) inflater.inflate(R.layout.view_passcode_entry, this);
this.addView(thisView, 0);

然后如果你想使用 view_passcode_entry 作为 pinView 的父级,那么你可以使用

 thisView.addView(pin, thisView.getChildCount());

是不是因为你已经在xml中提到了CustomPasscodeEntryView的高度(android:layout_height="40dp")?

你可以试试 wrap_content 吗?