Android layout 动态添加布局权重

Android layout 动态添加布局权重,android-layout,Android Layout,对于我的项目,我希望在我的页面中有30个图像,并且我希望以这样的方式排列它们,即在一个水平线性布局中有3个图像 因为我不能使用密度像素,因为它在不同的分区上看起来可能不同,所以我想使用布局权重。我们在XML文件中编写的方式: <ImageButton android:id="@+id/my_image" android:layout_width="wrap_content" android:layout_height="fill_parent

对于我的项目,我希望在我的页面中有30个图像,并且我希望以这样的方式排列它们,即在一个水平线性布局中有3个图像

因为我不能使用密度像素,因为它在不同的分区上看起来可能不同,所以我想使用布局权重。我们在XML文件中编写的方式:

<ImageButton
        android:id="@+id/my_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        **android:layout_weight="0.33"**
        android:background="@android:color/transparent"
        android:src="@drawable/ic_launcher" />


我想知道Java语言中XML代码的确切等价物是什么

可以使用视图的LayoutParams(最后一个参数)设置权重

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1.0f);
myView.setLayoutParams(params);