如何在android xml设计中定位元素?

时间:2017-03-20 00:25:46

标签: android xml android-layout

我正在构建一个消息应用程序Signal的分支,它具有完整的备份功能,而不是现在已经瘫痪的功能。但是,在信号4.0中更改了UI的设计之后,该功能无法正常工作。我从原始来源can be found here更改的所有文件。问题在于import_export_fragment.xml文件。原始文件是:

`

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

<LinearLayout android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_vertical"
              android:layout_gravity="center_vertical"
              android:padding="8dip">

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardElevation="3dp"
            card_view:cardCornerRadius="3dp"
            card_view:cardUseCompatPadding="true">

        <LinearLayout android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">

            <FrameLayout android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:padding="20dp">

                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/import_export_fragment__import"
                          android:textSize="20sp"/>

            </FrameLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/import_sms"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?import_sms"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/import_fragment__import_system_sms_database"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/import_fragment__import_the_database_from_the_default_system"/>

                </LinearLayout>
            </LinearLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/import_plaintext_backup"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?plaintext_backup"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/import_fragment__import_plaintext_backup"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/import_fragment__import_a_plaintext_backup_file"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
            android:layout_marginTop="20dp"
            card_view:cardElevation="3dp"
            card_view:cardCornerRadius="2dp"
            card_view:cardUseCompatPadding="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <LinearLayout android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">

            <FrameLayout android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:padding="20dp">

                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/import_export_fragment__export"
                          android:textSize="20sp"/>

            </FrameLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/export_plaintext_backup"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?plaintext_backup"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/export_fragment__export_plaintext_backup"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/export_fragment__export_a_plaintext_backup_compatible_with"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>

`

我更改了布局以包含用于加密备份/恢复的块:

`

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

<LinearLayout android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_vertical"
              android:layout_gravity="center_vertical"
              android:padding="8dip">

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardElevation="3dp"
            card_view:cardCornerRadius="3dp"
            card_view:cardUseCompatPadding="true">

        <LinearLayout android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">

            <FrameLayout android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:padding="20dp">

                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/import_export_fragment__import"
                          android:textSize="20sp"/>

            </FrameLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/import_sms"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?import_sms"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/import_fragment__import_system_sms_database"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/import_fragment__import_the_database_from_the_default_system"/>

                </LinearLayout>
            </LinearLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/import_plaintext_backup"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?plaintext_backup"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/import_fragment__import_plaintext_backup"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/import_fragment__import_a_plaintext_backup_file"/>
                </LinearLayout>
            </LinearLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/import_encrypted_backup"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?encrypted_backup"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/import_fragment__restore_encrypted_backup"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/import_fragment__restore_a_previously_exported_encrypted_signal_backup"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
            android:layout_marginTop="20dp"
            card_view:cardElevation="3dp"
            card_view:cardCornerRadius="2dp"
            card_view:cardUseCompatPadding="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <LinearLayout android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">

            <FrameLayout android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:padding="20dp">

                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/import_export_fragment__export"
                          android:textSize="20sp"/>

            </FrameLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/export_plaintext_backup"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?plaintext_backup"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/export_fragment__export_plaintext_backup"/>

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/export_fragment__export_a_plaintext_backup_compatible_with"/>
                </LinearLayout>
            </LinearLayout>

            <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/gray10"/>

            <LinearLayout android:id="@+id/export_encrypted_backup"
                          android:clickable="true"
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical"
                          android:paddingTop="8dip"
                          android:paddingBottom="8dip"
                          android:layout_marginLeft="16dip"
                          android:layout_marginRight="16dip">

                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginRight="10dip"
                           android:layout_marginEnd="10dip"
                           android:src="?encrypted_backup"/>

                <LinearLayout android:orientation="vertical"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content">

                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              style="@style/Registration.Description"
                              android:text="@string/export_fragment__export_encrypted_backup"/>


                    <TextView android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:gravity="start"
                              android:textAppearance="?android:attr/textAppearanceSmall"
                              android:text="@string/export_fragment__export_an_encrypted_backup_to_the_sd_card"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>

`

但这使得标题从屏幕上脱落:

Changed UI

我不知道为什么会这样。我可以纠正这个问题,以便用户通过添加一行来查看所有元素

android:layout_marginTop="160dp"

然而,对于第一个cardview,这导致边框在设备屏幕分辨率增加时增加:在低端设备上它看起来就在顶部,在具有更高分辨率的设备上我得到太多的空白顶部。

在底部,我也得到了太大的区域。

所以我的问题是:我缺少什么,以及如何为所有屏幕分辨率制作UI?

0 个答案:

没有答案