相对布局:以API和其中不同的行为; 11布局、不同、行为、API

2023-09-05 07:34:24 作者:船到桥头自然沉

我不知道为什么,但布局以及显示设备上与阿比11+,是不是老了。

I don't know why, but layout is shown well on device with Api 11+, isn't for older.

这是XML:

    <LinearLayout
        android:id="@+id/workers_linearlayout"
        android:layout_width="50dp"
        android:layout_height="70dp"
        android:layout_weight="1" >

        <RelativeLayout
            android:id="@+id/workers_relative_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/workers_small" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:scaleX="0.5"
                android:scaleY="0.5"
                android:src="@drawable/ic_cerchio_rosso"
                android:translationX="25dp"
                android:translationY="-20dp" />

            <TextView
                android:id="@+id/workers_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="9"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:translationX="25dp"
                android:translationY="-20dp" />
        </RelativeLayout>
    </LinearLayout>

这是导致在API 11 +:

This is result on API 11+:

这对API 10 - :

This on API 10-:

我试图修复它与布局玩,我能得到一个相当不错的结果,但从来没有像第一个。

I tried to fix it playing with layouts and I can obtain a quite good result, but never like the first one.

有人能帮助我吗?

编辑:

照片上的设备:

EDIT2

三角警告是:

字符串9应该使用字符串资源 ImageView1和3:缺少内容描述属性 RelativeLayout的还是它的父可能是无用的 在嵌套的权重是不好的性能

顺便说一句,没有这些固定的解决我的问题,我认为

By the way, nothing of these fixed solving my problem i think

推荐答案

确定。我修好了它。玩布置和跟随NikkyD的建议,关于中心父的特点,我遵循了这一方针:

Ok. I fixed it. Playing with layout and following NikkyD's suggestion about "center in parent" feature, i followed this policy:

这是不可能使用缩放和平移特性,因为旧的蜜蜂(可能)不认识他们。所以,我通过设定IV3(30dpx30dp)固定高度和宽度删除平移和缩放选项和缩放图像。现在,维是对的,但如果我叫allign母公司顶与allign父权的IV3和TextView的,他们的立场是好的,但TextView的不是定位在IV3中心。正是这样的:

It's not possible to use scale and translation properties because older Apis (maybe) don't recognize them. So, i deleted translation and scaling options and scaled image by setting a fixed height and width for IV3 (30dpx30dp). Now dimension is right, but if i call "allign parent Top" with "allign parent Right" for IV3 and textview, their position is good, but textview is not positioned at center of IV3. Exactly like this:

有关修复它,我增加了一个新的相对布局内workersRelativeLayout,我把它里面IV3和TextView的,并设置好的,为每一个中心父为TRUE。然后,我设置好的新的相对布局allign母公司顶和allign父权。这是最后的结果是:

For fixing it, i added a new relative layout inside "workersRelativeLayout" and i put inside it IV3 and TextView and setted, for each one, "center in parent" to TRUE. Then, i setted for new relative layout "allign parent Top" and "allign parent Right". This is final result:

这是新的XML布局:

            <LinearLayout
                android:id="@+id/workers_linearlayout"
                android:layout_width="50dp"
                android:layout_height="70dp"
                android:layout_weight="1" >

                <RelativeLayout
                    android:id="@+id/workers_relative_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:src="@drawable/workers_small" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true" >

                        <ImageView
                            android:id="@+id/imageView3"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:src="@drawable/ic_cerchio_rosso" />


                        <TextView
                            android:id="@+id/workers_number"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:text="9"
                            android:textAppearance="?android:attr/textAppearanceMedium" />

                    </RelativeLayout>
                </RelativeLayout>
            </LinearLayout>

希望这可以帮助别人:)

Hope this helps someone :)

修改

注重于母公司的宽度尺寸。如果设置权重的父1,根据显示器尺寸自然大小是动态的。图像定位始终在中心家长和IV3和TextView中相对布局会永远顶|右。所以,如果家长宽度尺寸增大,图像之间的距离为中心,新的相对布局过长,并可能发生这样的事情:

Pay attention to parent width size. If you set weight's parent to 1, naturally size is dynamic according to display size. Image is positioned always at center parent and relative Layout of IV3 and TextView will be always top|right. So if parent width size grows, distance between image centered and new relative layout grows too, and can happen something like this: