开发者社区 问答 正文

android这样的一个自定义View有什么问题吗?

不知道为什么在布局中添加了却没有什么显示:

//界面更新
BookButton bookButton=new BookButton(SelectAty.this,newNote);
 layout.addView(bookButton);

View源码:

public class BookButton extends RelativeLayout {
    Note note;
    TextView noteName;

    public BookButton(Context context,Note note){
        super(context);
        this.note=note;

        LayoutInflater.from(context).inflate(R.layout.book_button,this);
        noteName=(TextView)findViewById(R.id.noteName);
        noteName.setText(note.getNoteName());
    }

    //view的大小由note的大小决定
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        //没有效果?
        setMeasuredDimension(150+note.getPagesNumber(),1000);
    }

    public void setNoteName(String noteName){
        note.setNoteName(noteName);
        //更改textView,完成界面的更新
        this.noteName.setText(noteName);
    }

    public Note getNote(){
        return note;
    }

}

book_button.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/book1"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/noteName"
        android:layout_centerInParent="true"
        android:layout_width="15dp"
        android:layout_height="50dp" />
</RelativeLayout>

展开
收起
爵霸 2016-06-12 08:17:21 1927 分享 版权
1 条回答
写回答
取消 提交回答
  • setMeasuredDimension(150+note.getPagesNumber(),1000);
    这一行没有作用的话,请看看其中的参数是否合法?另外,看看有没有返回值?如果有,获取一下看看。

    2019-07-17 19:33:06
    赞同 展开评论