在线性布局中对齐元素

时间:2019-08-06 16:53:43

标签: java android layout android-linearlayout

我正在尝试重新创建使用 RelativeLayout,但我遇到了很多麻烦: enter image description here

我将新的LinearLayout分为两个子LinearLayout。在第一个中,我将图像和说明放在左边,表格放在文本的右边。在第二个LinearLayout中,我将放置最后一个表格和说明。这样做的样子是这样的: enter image description here 这是代码,我真的不明白怎么了:

    <?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>

1 个答案:

答案 0 :(得分:1)

为表创建两个单独的布局,并将其包括在主布局中,如下所示:

<?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. 将以下方法添加到您的Activity类:

    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);
    
    }
    

,然后将以下代码最终添加到onCreate:

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