<LinearLayout
android:layout_width="match_parent"
android:layout_height=" "
android:orientation="vertical" >
事情是这样的,假设我有一个宽度为 match_parent
的 LinearLayout
,所以 LinearLayout
的宽度取决于设备的宽度。我不知道的是如何设置 android:layout_height=""
值以对应于 android:layout_width="match_parent"
的值,这样我就可以有一个Square LinearLayout
我知道我可以使用 LayoutParams
做到这一点,但是有没有办法在 xml
上做到这一点?谢谢大家。。
最佳答案
先获取屏幕宽度
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width = mDisplay.getWidth();
int height = mDisplay.getHeight();
设置布局大小
LinearLayout layout = (LinearLayout)findViewById(R.id.LayoutID);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = width;
params.width = width;
layout.setLayoutParams(params);
在 XML 中:
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/LayoutID"
android:orientation="vertical" >
关于android - xml获取宽度并设置为android :height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438705/