【问题标题】:cannot dynamically add views android无法动态添加视图android
【发布时间】:2017-08-22 04:36:56
【问题描述】:

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

我的班级:

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"
/>

编辑:我活动中的视图:

    <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"/>

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

【问题讨论】:

  • 您想在自定义密码视图中做什么?动画什么?
  • 我想为每个新数字的输入设置动画(淡入),当输入新字母时,旧字母会转换为项目符号。 (就像在 Pixel / Nexus 锁屏中一样)
  • 好的,所以使用自定义android.text.method.TransformationMethod
  • 我一开始确实用过。它虽然没有动画,但直接与传递给TextViewCharsequencesStrings 一起工作(或者至少这是我从中推断出来的)。我对Spannables 不熟悉,所以决定改为这样做(它有效!:D)

标签: android android-layout android-view android-custom-view layout-inflater


【解决方案1】:

膨胀视图时使用了错误的视图 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());

【讨论】:

  • 不,不是这样。我在那里添加了它。对不起,我没有提到。
  • 有趣的是,我开始使用this,但应用程序崩溃时没有出现错误,所以我切换到thisView(现在看,它是STUPID)。现在我切换回this,它可以工作了。显然崩溃是由于我的应用程序的其他部分造成的。谢谢!
【解决方案2】:

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

你可以试试 wrap_content 吗?

【讨论】:

  • 我一开始尝试了 wrap_content。它没有用。我想使用developer options 中的show layout bounds 检查视图是否可见,所以我更改了它。布局在那里。没有添加孩子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
相关资源
最近更新 更多