【问题标题】:Android UI using XML (Android and Xml)使用 XML 的 Android UI(Android 和 Xml)
【发布时间】:2015-08-06 10:51:03
【问题描述】:

我想创建一个如下图所示的 UI。

它应该是这样的:(使用 Nexsus 5 从我的应用程序中截取的屏幕截图):

这就是它在 Nexsus S 设备(4 英寸)上的样子:

有人知道为什么会发生差异吗?这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2e2e2e"
    tools:context=".MainActivity"
    android:id="@+id/viewew">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="9"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginBottom="0dp"
            android:layout_weight="8"
            android:gravity="center_vertical"

            >

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

            <Button
                android:id="@+id/www"
                android:layout_weight="2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:background="@drawable/www"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                />

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

            <Button
                android:id="@+id/www"
                android:layout_weight="2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:background="@drawable/www"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                />

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

            <Button
                android:id="@+id/www"
                android:layout_weight="2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:background="@drawable/www"

                />

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

        </LinearLayout>

        <View
            android:id="@+id/idd"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:layout_alignParentBottom="true"

            android:layout_weight="2"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_centerHorizontal="true"
            android:text="Doesn't belong here?"
            android:layout_alignParentBottom="true"
            />

    </LinearLayout>



    <include layout="@layout/buttons" />





    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_centerHorizontal="true"
        android:text=" here?"
        android:paddingBottom="15dp"
        android:layout_alignParentBottom="true"
        android:textColor="#f7f7f7"
        android:id="@+id/tDoesntBelongHere"
        />

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:rotation_degrees="15.5"
        android:layout_above="@+id/eee"
        tools:context=".MyActivity" />


</RelativeLayout>

【问题讨论】:

    标签: java android xml user-interface


    【解决方案1】:

    在 android studio 上你必须为不同的设备设置屏幕尺寸。

    你的按钮有这个: android:layout_width="90dp" android:layout_height="90dp"

    因此,无论您使用哪个屏幕,尺寸始终为 90dp。

    在android studio中,您可以为同一页面创建更多xml页面:一个用于纵向,用于横向,ecc ecc,还有一个用于small-big-xxl ecc屏幕。

    查看来自 android 开发者网站的 this 链接。

    希望对你有帮助:)

    编辑:

    目前我正在使用的电脑中没有 Android Studio,所以我尝试解释一个可能的解决方案。

    设置边距而不是设置高度和宽度: 您可以从 botside 锚定所有内容,添加 TextView android:layout_alignParentBottom="true" (如果需要,还可以添加边距底部)。然后保持你的视图像现在一样保持400dp的静态高度,然后通过android:layout_above="@id/TextViewId将它锚定到TextView的顶部(它没有id,添加它)

    现在您必须对您的 LinearLayout 执行相同的操作:使用 android:layout_above="@id/idd 并删除静态大小。此时你可以让你的按钮设置height等于LinearLayout的height(当然你可以在按钮中设置margintop-bot)。

    对不起,如果我的英语不是那么好,我希望你得到解决方案

    【讨论】:

    • 感谢您的快速回复 :) 那么我该如何调用 4 英寸屏幕尺寸的目录?
    • 我看了,现在我的问题是 Nexus S 和 Nexus 5 属于“Layout-Normal”(大约 4 英寸)的同一类别,我该怎么办?
    • 谢谢!你帮了我很多! +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2018-01-04
    • 2018-01-30
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    相关资源
    最近更新 更多