2015-10-12 37 views

回答

0

您是否試過簡單指定高度爲match_parent而寬度爲wrap_content?具有相同值的父代。

0

將ImageView的高度設置爲match_parent,以便它與父級的高度相匹配。然後根據您嘗試實現的寬高比計算寬度,例如 - 16:9 =身高/ 0.5625。 - 並設置寬度。

編輯: 要獲得目標高度,然後計算基於該值的目標寬度,你將首先把父佈局的高度,然後計算寬度,然後設置你的ImageView的的LayoutParams,然後添加視圖。像這樣的東西...

RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.parent_layout); 
int height = parentLayout.getHeight(); 
int targetWidth = (int)Math.round(height/0.5625); //example for 16:9 ratio 
ImageView imageView = new ImageView(this); 
imageView.setLayoutParams(new ViewGroup.LayoutParams(
    targetWidth, 
    ViewGroup.LayoutParams.MATCH_PARENT 
)); 
parentLayout.addView(imageView); 
+0

如果我不知道長寬比?我期待着不同類型的圖像。 –

+0

然後在將高度設置爲match_parent後使用'wrap_content'作爲寬度,並且在設置高度後ImageView應將其自身尺寸設置爲正確的寬度。 – NoChinDeluxe

+0

這不起作用。這些值總是需要被定義。你可以設置一個然後設置另一個。 –

相關問題