How can I make complex TableLayout layout - Androi

2019-04-16 00:44发布

问题:

I wanted to make one layout for my application which is quite complex. I've decided to make it using TableLayout but I am unable to make it as I've planed. Can someone please tell me how it should be coded, because now it looks horrible.

This is my concept:

this is my code:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">

    <!-- ROW1 -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/r1v1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="7"
            android:text="7/7" />
    </TableRow>

    <!-- ROW2 -->
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/r2v1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r2v2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="2/7" />

        <TextView
            android:id="@+id/r2v3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="2/7" />

        <TextView
            android:id="@+id/r2v4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="2/7" />
    </TableRow>

    <!-- ROW3 -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/r3v1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r3v2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r3v3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r3v4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r3v5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r3v6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r3v7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />
    </TableRow>

    <!-- ROW4 -->
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/r4v1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r4v2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="5"
            android:text="5/7" />

        <TextView
            android:id="@+id/r4v3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />
    </TableRow>

    <!-- ROW5 -->
    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/r5v1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />

        <TextView
            android:id="@+id/r5v2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="3"
            android:text="3/7" />

        <TextView
            android:id="@+id/r5v3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="2/7" />

        <TextView
            android:id="@+id/r5v4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="1/7" />
    </TableRow>

    <!-- ROW6 -->
    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/r6v1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="7"
            android:text="7/7" />
    </TableRow>

</TableLayout>

and this is my outcome:

Not the bes, you have to admit :)

回答1:

for such a complex layout i'd write custom layout - a class that extends ViewGroup, like this:

import android.content.Context;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CustomLayout extends ViewGroup {
    private final static String TAG = "CustomLayout";

    private static final int NUM_CHILDREN = 20;
    private static final int GRID_WIDTH = 7;
    private static final int GRID_HEIGHT = 12;
    private static final float[] COORDS = {
        // row 0
        0, 0, 7, 1,
        // row 1
        0, 1, 1, 1, 
        1, 1, 2, 1,
        3, 1, 2, 1,
        5, 1, 2, 1,
        // row 2
        0, 2, 1, 1,
        1, 2, 1, 1,
        2, 2, 1, 1,
        3, 2, 1, 1,
        4, 2, 1, 1,
        5, 2, 1, 1,
        6, 2, 1, 1,
        // row 3
        0, 3, 1, 7,
        1, 3, 5, 7,
        6, 3, 1, 7,
        // row 4
        0, 10, 1, 1, 
        1, 10, 3, 1,
        4, 10, 2, 1,
        6, 10, 1, 1,
        // row 5
        0, 11, 7, 1,
    };

    public CustomLayout(Context context) {
        super(context);
        init();
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        Context ctx = getContext();
        setBackgroundColor(0xffaaaaaa);
        for (int i = 0; i < NUM_CHILDREN; i++) {
            TextView tv = new Button(ctx);
            tv.setBackgroundResource(android.R.drawable.btn_default_small);
            addView(tv);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = getWidth();
        int h = getHeight();
        for (int i = 0; i < NUM_CHILDREN; i++) {
            View v = getChildAt(i);
            int ii = i * 4;
            float fl = w * COORDS[ii] / GRID_WIDTH;
            float ft = h * COORDS[ii+1] / GRID_HEIGHT;
            float fr = fl + w * COORDS[ii+2] / GRID_WIDTH;
            float fb = ft + h * COORDS[ii+3] / GRID_HEIGHT;
            Log.d(TAG, "onLayout " + fl + " " + ft + " " + fr + " " + fb);
            v.layout(Math.round(fl), Math.round(ft), Math.round(fr), Math.round(fb));
        }
    }
}

to test it use this in your Activity's onCreate:

ViewGroup vg = new CustomLayout(this);
setContentView(vg);

as you can see it's not so difficult and it is way faster than using multiple LinearLayouts or RelativeLayout



回答2:

As I can see the best way to make your layout, is using LinearLayout

You can try to follow this steps.

  1. Create a parent with android:orientation="vertical"

  2. Inside your vertical Layout you should use horizontally to make each row don't forget android:width="fill_parent" to fill your complete line.

  3. And to define your line size and centered text your should use, android:weight="" android:layout_gravity=""