我需要在图像中显示像下面这样的4个图像视图。如下图所述,每个Imageview的宽度应该是屏幕宽度的2/5。我尝试使用LinearLayout,但没有像我预期的那样获得输出。

我的xml代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<ImageView
android:id="@+id/card1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/card2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/card3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/card4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
</LinearLayout>我该修改什么?
发布于 2018-04-27 12:40:17
public static int getScreenWidth(Context context) {
DisplayMetrics displaymetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay()
.getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
return width;
}使用上述方法计算屏幕宽度乘以屏幕宽度比值之后的屏幕宽度:
int imageViewWidth = getScreenWidth(this) * (2/5);现在将此宽度设置为您的imageView:
imageView.getLayoutParams().width = imageViewWidth;
imageView.requestLayout();发布于 2018-04-27 11:49:34
您需要使用weightsum的意味着您需要给出三个imageview作为weight="1",并将weight="2"分配给view图像视图。
最后,您需要在父布局中将您的总权重添加为weightsum="5"。因此,您可以获得屏幕宽度的2/5。。
请试一试,并将您的代码替换为:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="5"
android:orientation="horizontal">
<ImageView
android:id="@+id/card1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/card2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/card3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/card4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:scaleType="fitCenter" />
</LinearLayout>会对你有帮助的。
发布于 2018-04-27 11:52:01
我希望这对你有用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:padding="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/card1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/card2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/card3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/card4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
</LinearLayout>https://stackoverflow.com/questions/50061661
复制相似问题