如何使高度与相对宽度匹配?
How to make the height match the relative width?
所以我得到了这个 xml 代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="@drawable/profilbildfrau"
android:id="@+id/bild0"
>
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="@drawable/profilbildfrau"
android:id="@+id/bild1">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="@drawable/profilbildfrau"
android:id="@+id/bild2">
</ImageView>
</LinearLayout> (...)
这是我的 JavaCode:
ImageView beispiel;
ImageView[] profilbilder = new ImageView[21];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptmenue);
beispiel = (ImageView)findViewById(R.id.bild0);
int weite = beispiel.getWidth();
profilbilder[0] = (ImageView)findViewById(R.id.bild0);
profilbilder[1] = (ImageView)findViewById(R.id.bild1);
profilbilder[2] = (ImageView)findViewById(R.id.bild2);
profilbilder[3] = (ImageView)findViewById(R.id.bild3);
profilbilder[4] = (ImageView)findViewById(R.id.bild4);
profilbilder[5] = (ImageView)findViewById(R.id.bild5);
profilbilder[6] = (ImageView)findViewById(R.id.bild6);
profilbilder[7] = (ImageView)findViewById(R.id.bild7);
profilbilder[8] = (ImageView)findViewById(R.id.bild8);
profilbilder[9] = (ImageView)findViewById(R.id.bild9);
profilbilder[10] = (ImageView)findViewById(R.id.bild10);
profilbilder[11] = (ImageView)findViewById(R.id.bild11);
profilbilder[12] = (ImageView)findViewById(R.id.bild12);
profilbilder[13] = (ImageView)findViewById(R.id.bild13);
profilbilder[14] = (ImageView)findViewById(R.id.bild14);
profilbilder[15] = (ImageView)findViewById(R.id.bild15);
profilbilder[16] = (ImageView)findViewById(R.id.bild16);
profilbilder[17] = (ImageView)findViewById(R.id.bild17);
profilbilder[18] = (ImageView)findViewById(R.id.bild18);
profilbilder[19] = (ImageView)findViewById(R.id.bild19);
profilbilder[20] = (ImageView)findViewById(R.id.bild20);
for(int i = 0; i < 20; i++){
profilbilder[i].setMaxHeight(weite);
//ImageView bild = profilbilder[i];
//bild.getLayoutParams().height = weite;
profilbilder[i].setMinimumHeight(weite);
profilbilder[i].requestLayout();
}
}
我的目标是在其中一张图片的正确宽度适合屏幕后获取其宽度,然后将每张图片的高度设置为图片的宽度。尝试了一些代码,这是我最近的一个,但高度与宽度不匹配。我在这里错过了什么?
提前谢谢你,
朱利安
在 onCreate
中,视图尚未调整大小,通常宽度将为 return 0。将 OnGlobalLayoutListener
添加到视图并获取 [=13= 的高度和宽度]里面onGlobalLayout
。然后你可以为每个 ImageView
设置一个新的 LayoutParams
:
beispiel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
beispiel.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
beispiel.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
int width = beispiel.getWidth();
for(int i = 0; i <= 20; i++){
// make the height and width of each ImageView equal to width of beispiel
profilbilder[i].setLayoutParams(new LinearLayout.LayoutParams(width, width));
}
}
});
所以我得到了这个 xml 代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="@drawable/profilbildfrau"
android:id="@+id/bild0"
>
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="@drawable/profilbildfrau"
android:id="@+id/bild1">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="@drawable/profilbildfrau"
android:id="@+id/bild2">
</ImageView>
</LinearLayout> (...)
这是我的 JavaCode:
ImageView beispiel;
ImageView[] profilbilder = new ImageView[21];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptmenue);
beispiel = (ImageView)findViewById(R.id.bild0);
int weite = beispiel.getWidth();
profilbilder[0] = (ImageView)findViewById(R.id.bild0);
profilbilder[1] = (ImageView)findViewById(R.id.bild1);
profilbilder[2] = (ImageView)findViewById(R.id.bild2);
profilbilder[3] = (ImageView)findViewById(R.id.bild3);
profilbilder[4] = (ImageView)findViewById(R.id.bild4);
profilbilder[5] = (ImageView)findViewById(R.id.bild5);
profilbilder[6] = (ImageView)findViewById(R.id.bild6);
profilbilder[7] = (ImageView)findViewById(R.id.bild7);
profilbilder[8] = (ImageView)findViewById(R.id.bild8);
profilbilder[9] = (ImageView)findViewById(R.id.bild9);
profilbilder[10] = (ImageView)findViewById(R.id.bild10);
profilbilder[11] = (ImageView)findViewById(R.id.bild11);
profilbilder[12] = (ImageView)findViewById(R.id.bild12);
profilbilder[13] = (ImageView)findViewById(R.id.bild13);
profilbilder[14] = (ImageView)findViewById(R.id.bild14);
profilbilder[15] = (ImageView)findViewById(R.id.bild15);
profilbilder[16] = (ImageView)findViewById(R.id.bild16);
profilbilder[17] = (ImageView)findViewById(R.id.bild17);
profilbilder[18] = (ImageView)findViewById(R.id.bild18);
profilbilder[19] = (ImageView)findViewById(R.id.bild19);
profilbilder[20] = (ImageView)findViewById(R.id.bild20);
for(int i = 0; i < 20; i++){
profilbilder[i].setMaxHeight(weite);
//ImageView bild = profilbilder[i];
//bild.getLayoutParams().height = weite;
profilbilder[i].setMinimumHeight(weite);
profilbilder[i].requestLayout();
}
}
我的目标是在其中一张图片的正确宽度适合屏幕后获取其宽度,然后将每张图片的高度设置为图片的宽度。尝试了一些代码,这是我最近的一个,但高度与宽度不匹配。我在这里错过了什么?
提前谢谢你, 朱利安
在 onCreate
中,视图尚未调整大小,通常宽度将为 return 0。将 OnGlobalLayoutListener
添加到视图并获取 [=13= 的高度和宽度]里面onGlobalLayout
。然后你可以为每个 ImageView
设置一个新的 LayoutParams
:
beispiel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
beispiel.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
beispiel.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
int width = beispiel.getWidth();
for(int i = 0; i <= 20; i++){
// make the height and width of each ImageView equal to width of beispiel
profilbilder[i].setLayoutParams(new LinearLayout.LayoutParams(width, width));
}
}
});