मैंने एक दृश्य परिभाषित किया है, यह RelativeLayout
तक फैला है, मैं अपने बच्चों को निकालना चाहता हूं जिनकी आईडी लेआउट xml फ़ाइल में निर्दिष्ट है।एक दृश्य में findViewById बच्चे दृश्य देखने के लिए देखें
HeaderView.java
में:
1 package com.example.test;
2
3 public class HeaderView extends RelativeLayout {
4
5 public HeaderView(Context context, AttributeSet attrs) {
6 this(context, attrs, 0);
7
8 TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.HeaderView, 0, 0);
9
10 int headerId = 0;
11 int containerId = 0;
12
13 // find attributes
14 if (arr != null) {
15 if (arr.hasValue(R.styleable.HeaderView_headerview_header)) {
16 headerId = arr.getResourceId(R.styleable.HeaderView_headerview_header, 0);
17 }
18 if (arr.hasValue(R.styleable.HeaderView_headerview_container)) {
19 containerId = arr.getResourceId(R.styleable.HeaderView_headerview_container, 0);
20 }
21 arr.recycle();
22 }
23
24 Log.d("test", String.format("headerId: %s, containerId %s", headerId, containerId));
25 // headerId: 2131296260, containerId 2131296259
26
27 // here: mHeaderContainer is null
28 mHeaderContainer = (RelativeLayout) findViewById(headerId);
29 mContentViewContainer = (RelativeLayout) findViewById(containerId);
30 }
31 }
मैं headview_attrs.xml
में अपनी विशेषता परिभाषित:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HeaderView">
<attr name="headerview_header" format="reference" />
<attr name="headerview_container" format="reference" />
</declare-styleable>
</resources>
मैं activity_headerview.xml
में इस दृश्य का उपयोग एक लेआउट xml फ़ाइल में,:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.example.test"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.test.HeaderView
android:layout_width="match_parent"
android:layout_height="match_parent"
test:headerview_container="@+id/headerview_content"
test:headerview_header="@+id/headerview_header" >
<RelativeLayout
android:id="@+id/headerview_header"
android:layout_width="match_parent"
android:layout_height="30dp" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/headerview_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</com.example.test.HeaderView>
</RelativeLayout>
यहाँ समस्या आती है: HeaderView.java, line 28, 29, mHeaderContainer
औरमेंशून्य हैं।
यह activity_headerview.xml
में ऐसा लग रहा है, RelativeLayout
जिसका आईडी headerview_header
और RelativeLayout
जिसका आईडी है headerview_content
HeaderView
के बच्चे नहीं है।
क्या मैंने कुछ गलत किया? कोई भी सहायता अच्छी होगी!
आपकी मदद के लिए धन्यवाद, आप मेरा दिन बचाएं। – srain