tôi đã làm như sauLàm cách nào để chuyển tham chiếu chế độ xem sang chế độ xem tùy chỉnh của Android?
1) Tạo một styleable
<declare-styleable name="Viewee">
<attr name="linkedView" format="reference"/>
</declare-styleable>
2) xác định xem tùy chỉnh bố trí
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffc0">
<TextView
android:id="@+id/custom_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="[text]"
/>
</LinearLayout>
3) Tạo yêu cầu lớp
public class Viewee extends LinearLayout
{
public Viewee(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
View.inflate(context, R.layout.viewee, this);
TextView textView = (TextView) findViewById(R.id.custom_text);
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.Viewee);
int id = typedArray.getResourceId(R.styleable.Viewee_linkedView, 0);
if (id != 0)
{
View view = findViewById(id);
textView.setText(((TextView) view).getText().toString());
}
typedArray.recycle();
}
}
và cuối cùng trong hoạt động như bên dưới
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.ns"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="android"/>
<com.ns.Viewee
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:linkedView="@+id/tvTest"
/>
</LinearLayout>
nay mặc dù tôi nhận được một tổ chức phi zero id
trong Viewee constractor, findViewById(id)
retuns null và NullPointerException
occures.
Tôi đang thiếu gì?
KHÔNG đặt nó trong 'onDraw' nhưng trong' onAttachedToWindow'. Đặt nó trong onDraw sẽ được gọi là X lần trên giây, phương pháp, có thể là 60 lần mỗi giây. @DanielWilson bạn có thể xóa nhận xét của mình không? –