Android 如何设置宽度和高度的重量?
我需要创建具有宽度和高度的视图,它们是父视图大小的百分比。因此,我需要为一个视图的宽度和高度添加权重。 可能吗?Android 如何设置宽度和高度的重量?,android,view,height,width,android-layout-weight,Android,View,Height,Width,Android Layout Weight,我需要创建具有宽度和高度的视图,它们是父视图大小的百分比。因此,我需要为一个视图的宽度和高度添加权重。 可能吗? <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:weightSum="4" android:orientation="horizontal"> <ImageView android:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:weightSum="4"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/imgtype"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="3"
android:id="@+id/txtsubject"
android:textStyle="bold"
android:textSize="@dimen/lat_sub_text_size"
android:textColor="@android:color/black"/>
<LinearLayout/>
在这里,您可以将父linearlayout宽度的主要部分划分为textview和imageview的剩余次要部分。这是为了水平对齐,也可以对垂直对齐。父linearlayout的Weightsum属性决定它将包含的部分数。
在这里,您可以将父linearlayout宽度的主要部分划分为textview和imageview的剩余次要部分。这用于水平对齐,也可以对垂直对齐。父linearlayout的Weightsum属性决定其包含的部分数量。例如,我有以下布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_forImage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_forList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
与:
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
private static void setLayoutSize(View view, int width, int height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
例如,我有以下布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_forImage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_forList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
与:
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
private static void setLayoutSize(View view, int width, int height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
使用LinearLayout时,添加布局权重是一种很好的方法。
请记住,布局权重仅在线性布局的情况下有效,不能与相对布局一起使用。使用线性布局时,添加布局权重是一种很好的方法。
记住,布局重量仅在线性布局的情况下有效,不能与相对布局一起使用。您尝试过什么吗?是的。我试着把它放在线性布局(水平)中,以重量表示宽度,然后放在线性布局(垂直)中,以重量表示高度。但我认为这不是个好主意。你试过什么了吗?是的。我试着把它放在线性布局(水平)中,以重量表示宽度,然后放在线性布局(垂直)中,以重量表示高度。但我认为这不是一个好主意。