カスタムビューをリニアに追加しようとしていますプログラムによるレイアウトカスタムビューで、既に作成したレイアウトを膨らませたい。カスタムビューのインスタンスを作成して追加すると、ビューがある場所にスポットが表示されますが、レイアウトが膨らんでいないように見えます(カスタムレイアウトの背景色は緑色ですが、スペースに緑色、レイアウトの画像は表示されません)。
私のカスタムビュー:
public class AddressView extends View {
public AddressView(Context context) {
super(context);
View.inflate(context, R.layout.place_address, null);
}
public AddressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
View.inflate(context, R.layout.place_address, null);
}
public AddressView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.place_address, null);
}
}
私のカスタムビューレイアウト:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/address_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/house_1" />
</RelativeLayout>
活動における私のインスタンス化:
LinearLayout content = (LinearLayout) findViewById(R.id.content);
AddressView addressView = new AddressView(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 400);
content.addView(addressView, 0, lp);
私のR.id.content(線形レイアウトはスクロールビューの唯一の子です):
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/thumbnail_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/test_house" />
</FrameLayout>
</LinearLayout>
回答:
回答№1は5私は結局これをViewの代わりにFrameLayoutを拡張して渡し、 この 膨らませて電話します。ビューは追加されて膨らんでいると思いますが、子を正しくレイアウトする方法がわからなかったため、最初に親をinflate呼び出しに渡すと解決します。
public class AddressView extends FrameLayout {
public AddressView(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.place_address, this);
}
public AddressView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.place_address, this);
}
public AddressView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.place_address, this);
}
}