根据父级的父级尺寸设置宽度和高度 Android Studio
Set width and height based on dimentions of parent's parent Android Studio
我正在 LinearLayout 中动态添加 ImageViews 在 HorizontalScrollView 中。
现在我想根据这个HorizontalScrollView的父LinearLayout设置这些ImageView的宽高。
这是我的 xml (information_imagescrollview_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<HorizontalScrollView
android:id="@+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
这是我的代码:
View contentView = inflater.inflate(R.layout.information_imagescrollview_item, container, false);
LinearLayout scrollViewLayout = (LinearLayout) contentView.findViewById(R.id.linear);
for(Object intObj : page.content) {
ImageView imageView = new ImageView(this.getContext());
Drawable myDrawable = getResources().getDrawable((int) intObj);
imageView.setImageDrawable(myDrawable);
scrollViewLayout.addView(imageView);
}
page.addView(contentView);
如何将每个 imageView 设置为与 xml 中的父布局具有相同的宽度和高度?
可以通过代码更改动态视图的尺寸。所以我敢打赌,在将 ImageView
的布局参数添加到您的 LinearLayout 之前,您应该在 for
循环中更改它的布局参数,如下所示:
for(Object intObj : page.content) {
ImageView imageView = new ImageView(this.getContext());
Drawable myDrawable = getResources().getDrawable((int) intObj);
imageView.setImageDrawable(myDrawable);
//Changing height...
imageView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
//...and width
imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
scrollViewLayout.addView(imageView);
}
这会将 ImageView
的宽度和高度设置为包含(父级)LinearLayout
的宽度和高度。
我正在 LinearLayout 中动态添加 ImageViews 在 HorizontalScrollView 中。
现在我想根据这个HorizontalScrollView的父LinearLayout设置这些ImageView的宽高。
这是我的 xml (information_imagescrollview_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<HorizontalScrollView
android:id="@+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
这是我的代码:
View contentView = inflater.inflate(R.layout.information_imagescrollview_item, container, false);
LinearLayout scrollViewLayout = (LinearLayout) contentView.findViewById(R.id.linear);
for(Object intObj : page.content) {
ImageView imageView = new ImageView(this.getContext());
Drawable myDrawable = getResources().getDrawable((int) intObj);
imageView.setImageDrawable(myDrawable);
scrollViewLayout.addView(imageView);
}
page.addView(contentView);
如何将每个 imageView 设置为与 xml 中的父布局具有相同的宽度和高度?
可以通过代码更改动态视图的尺寸。所以我敢打赌,在将 ImageView
的布局参数添加到您的 LinearLayout 之前,您应该在 for
循环中更改它的布局参数,如下所示:
for(Object intObj : page.content) {
ImageView imageView = new ImageView(this.getContext());
Drawable myDrawable = getResources().getDrawable((int) intObj);
imageView.setImageDrawable(myDrawable);
//Changing height...
imageView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
//...and width
imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
scrollViewLayout.addView(imageView);
}
这会将 ImageView
的宽度和高度设置为包含(父级)LinearLayout
的宽度和高度。