android - xml获取宽度并设置为android :height

标签 android xml width

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height=" "
 android:orientation="vertical" >

事情是这样的,假设我有一个宽度为 match_parentLinearLayout,所以 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/

相关文章:

Android - 可靠地获取当前位置

java - 如何使用 Fragments 为 Android 创建二维码生成器

r - 使用 par 时图例框宽度不正确

java - 生成的 XML 中存在意外属性

Javascript 视口(viewport)然后 element.style.left 不会工作?这有什么问题

使用权重的按钮宽度的Android相对布局

android - 在 eclipse 中使用模拟数据不会产生警告

php - BasicNetwork.performRequest : Unexpected response code 500, 安卓注册系统

java - 一对多关联 "org.hibernate.InvalidMappingException: Could not parse mapping document from input stream"

java - 如何使两个edittext View 相互依赖