Android 如何在线性布局中确定布局权重?
在我的xml文件中,我将每个小圆圈(ImageView)设置为屏幕的25%,带有线性视图和布局权重。Android 如何在线性布局中确定布局权重?,android,xml,android-layout,format,android-linearlayout,Android,Xml,Android Layout,Format,Android Linearlayout,在我的xml文件中,我将每个小圆圈(ImageView)设置为屏幕的25%,带有线性视图和布局权重。 这是我手机的屏幕截图(它应该是什么样子的): 在我的平板电脑上,它的大小不会更改为屏幕的25%,这就是它的外观: 我的代码由三个线性布局组成,每个线性布局包含权重为0.25(共1个)的ImageView。 代码如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://sc
这是我手机的屏幕截图(它应该是什么样子的): 在我的平板电脑上,它的大小不会更改为屏幕的25%,这就是它的外观: 我的代码由三个线性布局组成,每个线性布局包含权重为0.25(共1个)的ImageView。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView12"
android:src="@drawable/circle_big"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.03125">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.09375">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.09375">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.03125">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:rotation = "120"
>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.03125">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4375">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.03125">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:rotation = "60"
>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.03125">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4375">
</FrameLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:id="@+id/imageView12"
android:src="@drawable/circle_white"
android:onClick="changeToScreenSelectLayout"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.03125">
</FrameLayout>
</LinearLayout>
</RelativeLayout>
不能将png文件用作白色圆圈。如果选择正确的android:scaleType,从技术上讲可以实现这一点,但它会导致可怕的锯齿效果 我会首先删除您现有的PNG(从每个资源桶),因为它们不能用于此。然后在drawable中创建一个名为“circle_white.xml”的新文件。把这个放进去,这会画出一个没有内在尺寸的圆。也就是说,它只会填充你渲染的任何内容
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#fff"/>
</shape>
除了HeightMatchesWidthView,您还可以使用PercentRelativeLayout,并使用它强制它们为正方形,以及正确的大小,而不必扩展视图来添加对强制正方形的支持。使用PercentRelativeLayout可能会有所帮助。据我所知,布局权重必须是完整的numbers@cricket_007错。它们可以是浮点值。@cricket_007根据定义,权重是floatsYeah。。。。人们有一种倾向,认为数字的总和应该是1。但是它的重量,1:1:1会产生与2.5:2.5:2.5相同的精确结果。这是图片中的白色圆圈,所以它是一个实际的PNG文件?是的,它是一个从ldpi到xxhdpi@sazzy4o请看这篇文章,我试着把它改成一个视图
public class HeightMatchesWidthView extends View {
public HeightMatchesWidthView(final Context context) {
super(context);
}
public HeightMatchesWidthView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public HeightMatchesWidthView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(width, width);
}
@Override
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
super.onSizeChanged(w, w, oldw, oldh);
}
}