【问题标题】:RelativeLayout inside LinearLayout is not shown perfectlyLinearLayout 内的RelativeLayout 没有完美显示
【发布时间】:2014-12-17 17:22:42
【问题描述】:

我正在关注我之前的问题:How to fit my Layout to any Screen in Android

在我的活动中,我有一个LinearLayout,里面有一堆RelativeLayouts 具有Board 的背景的LinearLayout 被设置在块的中心和内部所有RelativeLayoutImageViews

LinearLayoutRelativeLayouts三个部分的垂直布局:

  1. 名称和分数
  2. 9 个 x-o 块
  3. 游戏编号和倒计时

我使用9Patch Resizer Software 转换了我的MDPI 的所有照片,并全部保存在XXXHDPI(这是我正在使用的当前手机)

当我在我的 LG G3 手机 (XXXHDPI) 上运行活动时,所有其他活动都可以正常工作,除了下图中的这个活动:

LinearLayout 的背景不是在中心,所有的ImageViews 都被安排在 9 个空白区域的每一个上,而是被拉伸到屏幕的宽度,并且块到处都是。

但是当我从LinearLayout 中删除所有RelativeLayouts 时,LinearLayout 的板背景完美地显示在中心,这意味着图像到XXXHDPI 的转换很好。

(顺便提一下,在MDPI Emulator 中,它看起来很完美,就像这张照片一样:)

这是我的活动 XML 代码:

<?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"
android:background="@drawable/background"
android:gravity="center_horizontal|top" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|top"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/toplogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logottt" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/board"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/players_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="2dp"
                    android:text="@string/playersName"
                    android:textSize="30sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/players_score"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="150dp"
                    android:layout_marginTop="5dp"
                    android:text="@string/playersPoints"
                    android:textSize="25sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <TextView
                    android:id="@+id/machines_score"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="172dp"
                    android:layout_marginTop="5dp"
                    android:text="@string/machinePoints"
                    android:textSize="25sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <TextView
                    android:id="@+id/machines_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="50dp"
                    android:clickable="true"
                    android:onClick="openChat"
                    android:text="@string/machineName"
                    android:textSize="30sp" />
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >

            <ImageView
                android:id="@+id/block1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:onClick="touch"
                android:src="@drawable/block_1_empty" />

            <ImageView
                android:id="@+id/block2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="155dp"
                android:onClick="touch"
                android:src="@drawable/block_2_empty" />

            <ImageView
                android:id="@+id/block3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="310dp"
                android:onClick="touch"
                android:src="@drawable/block_3_empty" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp" >

            <ImageView
                android:id="@+id/block4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:onClick="touch"
                android:src="@drawable/block_4_empty" />

            <ImageView
                android:id="@+id/block5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:layout_marginTop="10dp"
                android:onClick="touch"
                android:src="@drawable/block_5_empty" />

            <ImageView
                android:id="@+id/block6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="310dp"
                android:layout_marginTop="10dp"
                android:onClick="touch"
                android:src="@drawable/block_6_empty" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="55dp" >

            <ImageView
                android:id="@+id/block7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:onClick="touch"
                android:src="@drawable/block_7_empty" />

            <ImageView
                android:id="@+id/block8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="155dp"
                android:onClick="touch"
                android:src="@drawable/block_8_empty" />

            <ImageView
                android:id="@+id/block9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="315dp"
                android:onClick="touch"
                android:src="@drawable/block_9_empty" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="6dp" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/game_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="105dp"
                    android:text="@string/gameNumber"
                    android:textSize="25sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/count_down"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="390dp"
                    android:text="@string/countDown"
                    android:textSize="25sp" />
            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/end_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/EndGame"
            android:onClick="endGame"
            android:src="@drawable/endgame_btn_1" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 你有很多RelativeLayouts,里面只有一个TextView。请删除那些RelativeLayouts,因为它们没有做任何额外的事情并使代码更加复杂。所以请用 替换 ,之后我会更好地帮助你。此外,您的根标签RelativeLayout 只有一个子标签:LinearLayout。外面的RelativeLayout没用。

标签: android layout android-linearlayout


【解决方案1】:

好的,我想出了很长的路要处理设备的密度,

density = getResources().getDisplayMetrics().density;

如果密度等于 4,这意味着 xxxhdpi 然后运行 ​​xxxhdpi 的 xml

if (density == 4) {
                setContentView(R.layout.activity_board_xxxhdpi);
            }

谢谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多