简体   繁体   English

将相对布局转换为线性布局

[英]Convert a Relative Layout to a Linear Layout

I'm developing an Android app, and for the graphic part I'm using the useful editor of Android Studio.我正在开发一个 Android 应用程序,对于图形部分,我正在使用 Android Studio 的有用编辑器。 This means that I'm moving the objects with the margin commands to make them fit the Google Pixel C screen perfectly.这意味着我正在使用边距命令移动对象,以使它们完美地适合 Google Pixel C 屏幕。 Now I tried the app on the tablet and, of course, it doesn't works.现在我在平板电脑上尝试了该应用程序,当然,它不起作用。 I read that I should be using a Linear Layout, but I think that the code I want to convert doesn't respect its rules, because I've elements on every row and in a different position.我读到我应该使用线性布局,但我认为我要转换的代码不遵守其规则,因为我在每一行和不同位置都有元素。 How can I represent it with a Linear Layout?如何用线性布局表示它? This is the code, as you can see it has a specified margin for each element.这是代码,正如您所看到的,每个元素都有指定的边距。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_main"
    android:scaleType = "centerCrop"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="1063dp"
        android:layout_height="476dp"
        android:layout_marginLeft="275dp"
        android:layout_marginTop="200dp"
        android:fontFamily="@font/kotta_one"
        android:text="Test del pensiero\n      divergente"
        android:textColor="#000000"
        android:textSize="100dp"
        android:textStyle="bold"
        tools:layout_editor_absoluteX="78dp"
        tools:layout_editor_absoluteY="123dp" />

    <Button
        android:id="@+id/button_1"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginLeft="400dp"
        android:layout_marginTop="480dp"
        android:background="@drawable/button_bg"
        android:text="@string/invioA"
        android:textAllCaps="false"
        android:textColor="#FFFFFF"
        android:textSize="18dp"
        app:layout_constraintVertical_bias="0.771" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginLeft="680dp"
        android:layout_marginTop="480dp"
        android:background="@drawable/button_bg"
        android:text="@string/invioB"
        android:textAllCaps="false"
        android:textColor="#FFFFFF"
        android:textSize="18dp"
        app:layout_constraintVertical_bias="0.771" />

    <Button
        android:id="@+id/button_3"
        android:layout_width="90dp"
        android:layout_height="70dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="22dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/kotta_one"
        android:text="Area\nDocenti"
        android:textAllCaps="false"
        android:textColor="#030000"
        android:textSize="24dp"
        app:layout_constraintVertical_bias="0.771" />

</RelativeLayout>

在此处输入图片说明

ok, try this:好的,试试这个:

<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:layout_height="wrap_content"
android:background="@drawable/bg"
android:orientation="vertical">

<Button
    android:id="@+id/button_3"
    android:layout_width="90dp"
    android:layout_height="70dp"
    android:background="@android:color/transparent"
    android:text="Area\nDocenti"
    android:textAllCaps="false"
    android:textColor="#030000"
    android:textSize="24dp"
    app:layout_constraintVertical_bias="0.771" />


<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="200dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:fontFamily="@font/kotta_one"
        android:text="Test del pensiero\n      divergente"
        android:textColor="#000000"
        android:textSize="100dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerInParent="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_1"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginRight="40dp"
            android:background="@android:color/black"
            android:text="Protocollo A"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            app:layout_constraintVertical_bias="0.771" />

        <Button
            android:id="@+id/button_2"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginLeft="40dp"
            android:background="@android:color/black"
            android:text="Protocollo B"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            app:layout_constraintVertical_bias="0.771" />

    </LinearLayout>
</RelativeLayout>

I think your best option would be to use ConstraintLayout : read about it here我认为你最好的选择是使用ConstraintLayout在这里阅读

but something quick and hacky for LinearLayout:但是对于 LinearLayout 来说,一些快速而棘手的事情:

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

    <Button
        android:id="@+id/button_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_margin_start="20dp"
        android:background="@android:color/transparent"
        android:fontFamily="@font/kotta_one"
        android:text="Area\nDocenti"
        android:textAllCaps="false"
        android:textColor="#030000"
        android:textSize="24dp"/>

   <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:layout_gravity="centre"
     android:gravity="centre">
       <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/kotta_one"
        android:text="Test del pensiero"
        android:textColor="#000000"
        android:textSize="100dp"
        android:textStyle="bold"/>
      <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/kotta_one"
        android:text="divergente"
        android:textColor="#000000"
        android:textSize="100dp"
        android:textStyle="bold"/>

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

                 <Button
                   android:id="@+id/button_1"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@drawable/button_bg"
                   android:text="@string/invioA"
                   android:textAllCaps="false"
                   android:textColor="#FFFFFF"
                   android:textSize="18dp"/>

                <Button
                   android:id="@+id/button_2"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@drawable/button_bg"
                   android:text="@string/invioB"
                   android:textAllCaps="false"
                   android:textColor="#FFFFFF"
                   android:textSize="18dp"/>
      </LinearLayout>
   </LinearLayout>
</LinearLayout>

That might not be perfect as I just wrote it free hand, but I think it gives you a good idea of what you can do.这可能并不完美,因为我只是徒手写的,但我认为它可以让您对自己可以做什么有一个很好的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM