我想将我的LinearLayout (或视图)调整为相对于父级或其自身的维度。例如,我希望宽度是父对象宽度的1/3。或者,高度应该与它自己的宽度相同。我不想使用任何常量,这样它就可以适用于所有设备。
添加代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout android:id="@+id/ll_board"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
...
</LinearLayout>
代码:
public class GMActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
LinearLayout board_layout = (LinearLayout)findViewById(R.id.ll_board);
// I wanted to resize board_layout here ..
// getParent().getWidth() returns 0
Log.d("gm", "layout: " + ((LinearLayout) board_layout.getParent()).getWidth());
// ..
}
}
getWidth()返回0。现在这么说是不是太早了?如果是,那么正确的地方应该是什么呢?
基本上,我的目的是使布局的宽度是屏幕大小宽度的一部分,高度与其自身的宽度相同。
发布于 2012-06-23 17:49:29
考虑到布局您的LinearLayout和它的父级,它是另一个LinearLayout:
获取父对象的宽度:
int parentWidth = ((LinearLayout) layout.getParent()).getWidth();
获取视图的宽度:
int viewWidth = ((LinearLayout) layout).getWidth();
设置
view.setHeight(viewWidth );
view.setWidth(parentWidth / 3);
发布于 2012-06-24 20:20:22
我在LinearLayout in Square Form中找到了height=width解决方案(方形布局)
https://stackoverflow.com/questions/11168431
复制相似问题