this guideの「複合ビューを作成する」の部分に従ってきました。私のアプリケーションでの活動の一つでは以下のように定義されたビューがあります:レイアウトXMLファイルのカスタムビュークラスタグで「include」タグを置き換えると、ビューが表示されません。なぜですか?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some other subviews... -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<!-- some other subviews... -->
<include
android:id="@+id/myHeaderView"
layout="@layout/view_my_header"></include>
</FrameLayout>
</LinearLayout>
カスタムmyHeaderView
は、以下のようなXMLファイルで定義されたそのレイアウトになっていますが:
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="250dip"
android:orientation="vertical"
android:background="@android:color/transparent">
<View
android:layout_height="5dip"
android:layout_width="match_parent"
app:layout_aspectRatio="100%"
android:background="@android:color/holo_green_light" />
<View
android:id="@+id/yellowView"
app:layout_heightPercent="50%"
app:layout_aspectRatio="100%"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@android:color/holo_orange_light"
android:layout_marginBottom="12dp"
android:layout_marginLeft="12dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<View
android:id="@+id/redView"
app:layout_heightPercent="40%"
app:layout_aspectRatio="200%"
android:background="@android:color/holo_red_light"
android:layout_width="200dip"
android:layout_height="100dip"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<View
android:layout_height="5dip"
android:layout_width="match_parent"
app:layout_aspectRatio="100%"
android:background="@android:color/holo_green_light"
android:layout_alignParentBottom="true"/>
</android.support.percent.PercentRelativeLayout>
基本的に、それはのように見えるべきですこの:
これはよく実行され、期待通りにカスタムビューが表示されています。しかし、私は、次のいずれかで、XMLでカスタムビュー要素のinclude
タグを交換する場合:以下のようにカスタムクラスMyHeaderView
のための私のコンストラクタを
<com.myDomain.myHeaderView
android:id="@+id/myHeaderView"
android:layout_width="match_parent"
android:layout_height="250dip"
android:orientation="vertical"
android:background="@android:color/transparent"/>
およびUPDATE:
public MyHeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
// some other constructor implementations, which they all call "initView(context)"
private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_my_header, this, true);
}
サブビュー(カスタムヘッダービューの黄色いビューなど)が消えてしまいます。 Device Monitorのレイアウトビューで確認したところ、ヘッダービューとそのサブビューのサイズは正常です。しかし、私が知らない何らかの理由で、彼らは見えません。
何か間違っていますか? include
タグは対応するファイルで定義されているレイアウトコンポーネントを再使用していることを示していますが、ViewGroup
の代わりにMyHeaderView
というカスタムビューを参照したいので、それで定義されたメソッドを呼び出すことができますビュー、どのように私はこれを達成することができますか?
ありがとうございます!
感謝を!私はあなたが示唆したように達成しましたが、id属性は重複していますか? – alanlo
あなたは正しいです。私はそれを編集した。 – shhp