【问题标题】:Android normal screen size cropped on bottomAndroid 正常屏幕尺寸在底部裁剪
【发布时间】:2014-10-24 23:40:22
【问题描述】:

我已经使用相对布局创建了一个具有 hdpi 密度的正常屏幕尺寸的布局。但我知道一些具有相同屏幕尺寸的设备,底部被裁剪为 7-8 毫米,用于 3 个 android 常用按钮(主页等)。

现在我想知道如何创建适合该类型设备的布局,因为现在它会将我的布局裁剪为 7 毫米?

谢谢。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/prime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8B008B"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
tools:context="com.example.testznanja.MainActivity" >

<RelativeLayout
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/question_layout" >

    <TextView
        android:id="@+id/questionText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

<Button
    android:id="@+id/beginTest"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:layout_below="@+id/infoPanel"
    android:layout_marginTop="25dp"
    android:background="@drawable/button_normal"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/beginTest"
    android:layout_marginTop="18dp"
    android:background="@drawable/button_normal"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button2"
    android:layout_marginTop="18dp"
    android:background="@drawable/button_normal"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button3"
    android:layout_marginTop="18dp"
    android:background="@drawable/button_normal"
    android:text="Button" />

<RelativeLayout
    android:id="@+id/infoPanel"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_below="@+id/rl"
    android:layout_marginTop="25dp"
    android:background="@drawable/info_panel_layout"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <TextView
        android:id="@+id/questionsAnswers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Skor: 0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="right"
        android:text="Vreme: 20"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/help"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="50:50"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

【问题讨论】:

  • 你能把你的xml代码贴出来让我们看看问题吗?

标签: android layout size screen


【解决方案1】:

您是否尝试过在带有诸如 nexus 系列之类的软菜单按钮的模拟器上对其进行测试?这些手机的宽高比与 Galaxy s3、s4 等其他手机相差不远。但要回答您的问题,如果您真的希望您的布局对所有内容都灵活,我可以想到 3 个选项:

[1] 如果您不介意滚动,您可以将父级 RelativeLayout(即 @+id/prime)包装在 ScrollView 内。或者看看我修改的你的xml文件。只需将可绘制对象替换为您提供的对象即可:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/prime"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#8B008B"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="5dp"
        tools:context="com.example.testznanja.MainActivity" >

        <RelativeLayout
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@drawable/question_layout" >

            <TextView
                android:id="@+id/questionText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </RelativeLayout>

        <Button
            android:id="@+id/beginTest"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_below="@+id/infoPanel"
            android:layout_marginTop="25dp"
            android:background="@drawable/button_normal"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/beginTest"
            android:layout_marginTop="18dp"
            android:background="@drawable/button_normal"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button2"
            android:layout_marginTop="18dp"
            android:background="@drawable/button_normal"
            android:text="Button" />

        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button3"
            android:layout_marginTop="18dp"
            android:background="@drawable/button_normal"
            android:text="Button" />

        <RelativeLayout
            android:id="@+id/infoPanel"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_below="@+id/rl"
            android:layout_marginTop="25dp"
            android:background="@drawable/info_panel_layout"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >

            <TextView
                android:id="@+id/questionsAnswers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="Skor: 0"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/timer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:gravity="right"
                android:text="Vreme: 20"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="50:50"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </RelativeLayout>

        <View
            android:id="@+id/dummy"
            android:layout_width="match_parent"
            android:layout_height="800dp"
            android:layout_below="@+id/button4"
            android:background="#ff4444" />
    </RelativeLayout>

</ScrollView>

请注意,我添加了一个带有红色背景的View,并在底部添加了android:id="@+id/dummy"。这是为了让您看到滚动效果并模拟裁剪的场景。如果你不需要它,只需将其删除。

[2] 如果您希望所有内容都适合屏幕而不需要向下滚动,您应该将父级 RelativeLayout 转换为每个子级的 LinearLayoutuse weights。这种方法更复杂并且需要大量工作,因为您必须计算每个孩子的重量,并且您必须在已有的 xml 中更改孩子的每个宽度和高度。

[3] 重新设计您的布局,使其始终有额外的空间来适应不同的设备纵横比,并重新设计您的图像和背景。我建议你阅读Android Design Guidelines

第一个是 3 个中最简单的一个。如果您希望布局尽可能灵活,我的朋友很抱歉,但我想不出更简单的方法。

【讨论】:

  • 我不需要模拟器,我的手机有软键,所以我在真机上试了一下,我的布局被他们裁剪了。我找到了一个制作不同布局(layoutWithKeys.xml)的解决方案,然后我检查设备是否有软键,这取决于我使用一种或其他布局设置内容视图。不过感谢您尝试帮助我交配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多