如何将线性布局转换为约束布局?

时间:2020-02-11 13:17:53

标签: java android android-constraintlayout

我已经开发了一个应用程序,并且在每个活动中都使用了线性布局。每个活动的布局看起来都与我在手机(Nokia 5.1 plus)上运行时在android studio中设计的布局完全相同。但是,当我在另一台小型手机上运行该应用程序时,例如三星J1布局被弄乱了。一些按钮,图像不在屏幕上。因此要解决此问题,我的意思是使每个设备的布局都相同,我可以使用约束布局吗? / p>

实际上,我尝试通过右键单击组件树将布局转换为约束布局......但是转换后看起来并不一样。一切都搞砸了。

所以我想知道的是, 如何使每个设备的布局看起来都一样?

如果该方法是将其转换为约束布局,那么如何在没有我提到的最后一个问题的情况下实现呢?

这是我的活动的xml代码之一。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".alkaned">

    <ImageView
        android:id="@+id/ivBack3"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="375dp"
        android:clickable="true"
        app:srcCompat="@drawable/back" />

    <ImageView
        android:id="@+id/imageView198"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginTop="75dp"
        app:srcCompat="@drawable/alkanesnew" />

    <androidx.cardview.widget.CardView
        android:id="@+id/btnAlkaneM"
        android:layout_width="match_parent"
        android:layout_height="60dp"

        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView401"
                android:layout_width="397dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="0dp"
                android:layout_weight="1"
                android:fontFamily="@font/newfont"
                android:text="we,aflak idod.kakd wdldr "
                android:textColor="#000000"
                android:textSize="30sp" />

            <ImageView
                android:id="@+id/imageView209"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/milanpng" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btnAlkaneS"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView405"
                android:layout_width="397dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="0dp"
                android:layout_weight="1"
                android:fontFamily="@font/newfont"
                android:text="we,aflak Ndckh jk m%;sl%shd"
                android:textColor="#000000"
                android:textSize="30sp" />

            <ImageView
                android:id="@+id/imageView212"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/milanpng" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btnAlkaneR"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView406"
                android:layout_width="397dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:fontFamily="@font/newfont"
                android:text="we,aflak iïnkao mßj¾;k yd m%;sl%shd idrdxYh"
                android:textColor="#000000"
                android:textSize="30sp" />

            <ImageView
                android:id="@+id/imageView215"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/milanpng" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可以使用库在不同大小的设备上处理UI。

尝试使用SDP库。有关更多信息和用法,您可以通过here

进行检出

将其添加到您的Gradle中以使用SDP库。

dependencies {
  implementation 'com.intuit.sdp:sdp-android:1.0.6'
}

在您的布局中,使用dp代替

而不是spd

代替

android:layout_width =“ 35dp”

使用

android:layout_width="@dimen/_35sdp"

答案 1 :(得分:0)

即使在ConstraintLayout上,控件如您所说的那么小也将不在屏幕上。要解决此问题,您应在LinearLayout之前实现例如 ScrollView ,如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    tools:context=".alkaned">

    <ImageView
        android:id="@+id/ivBack3"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="375dp"
        android:clickable="true"
        app:srcCompat="@drawable/back" />

    <ImageView
        android:id="@+id/imageView198"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginTop="75dp"
        app:srcCompat="@drawable/alkanesnew" />

    <androidx.cardview.widget.CardView
        android:id="@+id/btnAlkaneM"
        android:layout_width="match_parent"
        android:layout_height="60dp"

        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView401"
                android:layout_width="397dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="0dp"
                android:layout_weight="1"
                android:fontFamily="@font/newfont"
                android:text="we,aflak idod.kakd wdldr "
                android:textColor="#000000"
                android:textSize="30sp" />

            <ImageView
                android:id="@+id/imageView209"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/milanpng" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btnAlkaneS"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView405"
                android:layout_width="397dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="0dp"
                android:layout_weight="1"
                android:fontFamily="@font/newfont"
                android:text="we,aflak Ndckh jk m%;sl%shd"
                android:textColor="#000000"
                android:textSize="30sp" />

            <ImageView
                android:id="@+id/imageView212"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/milanpng" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btnAlkaneR"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView406"
                android:layout_width="397dp"
                android:layout_height="70dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:fontFamily="@font/newfont"
                android:text="we,aflak iïnkao mßj¾;k yd m%;sl%shd idrdxYh"
                android:textColor="#000000"
                android:textSize="30sp" />

            <ImageView
                android:id="@+id/imageView215"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/milanpng" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
</LinearLayout>
</ScrollView>