简体   繁体   中英

Align elements in Linear Layout

I'm trying to recreate this layout I made with the RelativeLayout but I'm having a lot of troubles: 在此处输入图片说明

I divided the new LinearLayout into two sub-LinearLayout. In the first one I would place the Image and the description on the left, and the table with the text on the right. In the second LinearLayout I would put the last table and description. This is what it looks like by doing this: 在此处输入图片说明 This is the code, I really don't understand what's wrong:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/slidelinearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_main2"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
    android:layout_width="640dp"
    android:layout_height="438dp"
    android:layout_gravity="left"
    android:layout_weight="0.01"
    android:gravity="left"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/slideimg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="124dp"
        android:layout_marginTop="144dp"
        tools:layout_conversion_absoluteHeight="380dp"
        tools:layout_conversion_absoluteWidth="420dp"
        tools:layout_editor_absoluteX="220dp"
        tools:layout_editor_absoluteY="120dp" />

    <TextView
        android:id="@+id/txttitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/slideimg"
        android:layout_alignEnd="@+id/slideimg"
        android:padding="16dp"
        android:text="Title Here"
        android:textColor="#000000"
        android:textSize="30sp"
        android:textStyle="bold"
        tools:layout_conversion_absoluteHeight="73dp"
        tools:layout_conversion_absoluteWidth="166dp"
        tools:layout_editor_absoluteX="330dp"
        tools:layout_editor_absoluteY="480dp" />

    <TextView
        android:id="@+id/t1"
        android:layout_width="249dp"
        android:layout_height="79dp"
        android:layout_above="@+id/tl"
        android:layout_alignEnd="@+id/tl"
        android:layout_marginEnd="13dp"
        android:layout_marginBottom="-32dp"
        android:fontFamily="@font/sriracha"
        android:text="Punteggio finale"
        android:textColor="#DD1717"
        android:textSize="30dp"
        tools:layout_conversion_absoluteHeight="61dp"
        tools:layout_conversion_absoluteWidth="217dp"
        tools:layout_editor_absoluteX="760dp"
        tools:layout_editor_absoluteY="150dp" />

    <TextView
        android:id="@+id/t"
        android:layout_width="wrap_content"
        android:layout_height="57dp"
        android:layout_alignTop="@+id/t1"
        android:layout_alignEnd="@+id/t1"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="33dp"
        android:fontFamily="@font/sriracha"
        android:text="______________"
        android:textColor="#020101"
        android:textSize="30dp"
        tools:layout_conversion_absoluteHeight="61dp"
        tools:layout_conversion_absoluteWidth="210dp"
        tools:layout_editor_absoluteX="765dp"
        tools:layout_editor_absoluteY="153dp" />

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/tl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/t2"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="265dp"
        android:layout_marginBottom="86dp"
        android:background="@android:color/white"
        android:padding="18dp"
        tools:context=".Result"
        tools:layout_conversion_absoluteHeight="260dp"
        tools:layout_conversion_absoluteWidth="316dp"
        tools:layout_editor_absoluteX="220dp"
        tools:layout_editor_absoluteY="190dp">

        <TableRow
            android:id="@+id/tr_item_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Flessibilità"
                android:textColor="#030101"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/f_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Fluidità"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/fl_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Originalità"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/o_item_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Elaborazione"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/el_item_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/item_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="  Titolo"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/t_item_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>
    </TableLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="408dp"
    android:layout_height="279dp">

    <TextView
        android:id="@+id/t4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/tl_2"
        android:layout_alignBottom="@+id/t1"
        android:layout_gravity="right"
        android:layout_marginEnd="-1dp"
        android:layout_marginBottom="-478dp"
        android:fontFamily="@font/sriracha"
        android:gravity="right"
        android:text="______________________"
        android:textColor="#020101"
        android:textSize="30dp"
        tools:layout_conversion_absoluteHeight="115dp"
        tools:layout_conversion_absoluteWidth="239dp"
        tools:layout_editor_absoluteX="785dp"
        tools:layout_editor_absoluteY="532dp" />

    <TableLayout
        android:id="@+id/tl_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/tl"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="193dp"
        android:layout_marginEnd="-248dp"
        android:layout_marginBottom="26dp"
        android:background="@android:color/white"
        android:padding="25dp"
        tools:context=".Result"
        tools:layout_conversion_absoluteHeight="208dp"
        tools:layout_conversion_absoluteWidth="314dp"
        tools:layout_editor_absoluteX="710dp"
        tools:layout_editor_absoluteY="560dp">

        <TableRow
            android:id="@+id/tr_item_6"
            android:layout_width="222dp"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/t1_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="    Tempo di reazione"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/tempo_item_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/t2_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="    Tempo di completamento"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/tempo_item_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_item_8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="2dp">

            <TextView
                android:id="@+id/n_cancellature"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingRight="10dp"
                android:text="    Numero cancellature"
                android:textColor="#070404"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/n_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/table_cell_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="#070404"
                android:textSize="30dp" />
        </TableRow>
    </TableLayout>

    <TextView
        android:id="@+id/t2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tl_2"
        android:layout_marginEnd="-408dp"
        android:layout_marginBottom="166dp"
        android:layout_toStartOf="@+id/tl_2"
        android:fontFamily="@font/sriracha"
        android:text="Considerazioni aggiuntive"
        android:textColor="#DD1717"
        android:textSize="30dp"
        tools:layout_conversion_absoluteHeight="115dp"
        tools:layout_conversion_absoluteWidth="244dp"
        tools:layout_editor_absoluteX="780dp"
        tools:layout_editor_absoluteY="530dp" />

</LinearLayout>

Create the two separate layouts for the tables and include it in the main layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg">

<LinearLayout
    android:id="@+id/upperLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="140dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="400dp"
            android:layout_height="350dp"
            android:layout_marginRight="150dp"
            android:background="@android:color/darker_gray"
            android:src="@drawable/ic_launcher_link" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="20dp"
            android:text="Senza nome"
            android:textColor="@android:color/black"
            android:textSize="40sp"
            android:textStyle="bold" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/upperTableHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="160dp"
            android:text="Table Header here"
            android:textColor="@android:color/holo_red_light"
            android:textSize="30sp" />

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

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/upperLayout"
    android:layout_alignParentRight="true"
    android:layout_marginTop="30dp"
    android:layout_marginRight="80dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lowerTableHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Table Header here"
        android:textColor="@android:color/holo_red_light"
        android:textSize="30sp" />

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

</LinearLayout>

</RelativeLayout>
  1. add the following methods to your Activity class:

     public int getWidth() { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int width = displayMetrics.widthPixels; return width; } public void setLayoutMargins() { View layout = findViewById(R.id.relative); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layout.getLayoutParams()); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); layoutParams.topMargin = 400; layout.setLayoutParams(layoutParams); //ImageView Layout View image = findViewById(R.id.imgage); LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(image.getLayoutParams()); image.getLayoutParams(); imageParams.width = 800; imageParams.height = 600; image.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark)); image.setLayoutParams(imageParams); //UpperTable Layout View upperTable = findViewById(R.id.upperTableLayout); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(upperTable.getLayoutParams()); params.leftMargin = 250; params.topMargin = 160; upperTable.setLayoutParams(params); } public void setTextSizes() { txt1 = findViewById(R.id.item_1); txt2 = findViewById(R.id.item_2); txt3 = findViewById(R.id.item_3); txt4 = findViewById(R.id.item_4); txt5 = findViewById(R.id.item_5); f_item_1 = findViewById(R.id.f_item_1); f_item_2 = findViewById(R.id.fl_item_2); t1_item_1 = findViewById(R.id.t1_item_1); tempo_item_1 = findViewById(R.id.tempo_item_1); t2_item_2 = findViewById(R.id.t2_item_2); tempo_item_2 = findViewById(R.id.tempo_item_2); n_cancellature = findViewById(R.id.n_cancellature); n_3 = findViewById(R.id.n_3); txtSenza = findViewById(R.id.txtSenza); txtUpper = findViewById(R.id.upperTableHeader); txtLower = findViewById(R.id.lowerTableHeader); txt1.setTextSize(30); txt2.setTextSize(30); txt3.setTextSize(30); txt4.setTextSize(30); txt5.setTextSize(30); f_item_1.setTextSize(30); f_item_2.setTextSize(30); txtSenza.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40); txtLower.setTextSize(30); txtUpper.setTextSize(30); t1_item_1.setTextSize(30); t2_item_2.setTextSize(30); tempo_item_1.setTextSize(30); tempo_item_2.setTextSize(30); n_3.setTextSize(30); n_cancellature.setTextSize(30); } 

and then finally add the following code to onCreate:

     if (getWidth() > 2000) {
        setLayoutMargins();
        setTextSizes();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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