2015-01-08 2 views
1

В моем приложении у меня есть активность, как на рисунке (рисунок # 1). В TableLayout я бы построил таблицу с n строками и 6 столбцами. Я строю таблицу вставки, как и список, n LinearLayout, и каждый из них содержит 6 TextView (значения таблицы) и 5 ​​TextView, которые действуют как пробелы между столбцами. Я написал это, custom_linearlayout.xml:layout_weight не работает

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/custom_linearLayout" > 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="4" 
     android:id="@+id/titolo1" 
     android:text="tit1" 
     android:background="#4584d3" 
     /> 
    <TextView 
     android:layout_width="2dp" 
     android:layout_height="match_parent" 
     android:id="@+id/spazio1" 
     /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/titolo2" 
     android:text="tit2" 
     android:background="#4584d3" 
     /> 
    <TextView 
     android:layout_width="2dp" 
     android:layout_height="match_parent" 
     android:id="@+id/spazio2" 
     /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/titolo3" 
     android:text="tit3" 
     android:background="#4584d3" 
     /> 
    <TextView 
     android:layout_width="2dp" 
     android:layout_height="match_parent" 
     android:id="@+id/spazio3" 
     /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/titolo4" 
     android:text="tit4" 
     android:background="#4584d3" 
     /> 
    <TextView 
     android:layout_width="2dp" 
     android:layout_height="match_parent" 
     android:id="@+id/spazio4" 
     /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/titolo5" 
     android:text="tit5" 
     android:background="#4584d3" 
     /> 
    <TextView 
     android:layout_width="2dp" 
     android:layout_height="match_parent" 
     android:id="@+id/spazio5" 
     /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/titolo6" 
     android:text="tit6" 
     android:background="#4584d3" 
     /> 
</LinearLayout> 

Рендеринг должно быть что-то вроде этого: (рисунок 2 #)

Это Java-код для использования макета:

package blablabla.myapplication; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import blablabla.myapplication.R; 

public class Riga extends LinearLayout { 
    TextView colonna1, colonna2 , colonna3, colonna4, colonna5, colonna6; 

    public Riga (Context contesto) { 
     super (contesto); 
     init(); 
    } 

    private void init() { 
     LinearLayout ll = (LinearLayout) LayoutInflater.from (getContext()).inflate(R.layout.custom_linearlayout , null); 
     colonna1 = (TextView)ll.findViewById(R.id.titolo1); 
     colonna2 = (TextView)ll.findViewById(R.id.titolo2); 
     colonna3 = (TextView)ll.findViewById(R.id.titolo3); 
     colonna4 = (TextView)ll.findViewById(R.id.titolo4); 
     colonna5 = (TextView)ll.findViewById(R.id.titolo5); 
     colonna6 = (TextView)ll.findViewById(R.id.titolo6); 
     addView(ll); 
    } 

    public void setVal1 (String s) { 
     colonna1.setText(s); 
    } 

    public void setVal2 (String s) { 
     colonna2.setText(s); 
    } 

    public void setVal3 (String s) { 
     colonna3.setText(s); 
    } 

    public void setVal4 (String s) { 
     colonna4.setText(s); 
    } 

    public void setVal5 (String s) { 
     colonna5.setText(s); 
    } 

    public void setVal6 (String s) { 
     colonna6.setText(s); 
    } 

    public void setValues (String [] array) { 
     if (array.length == 6) { 
      colonna1.setText(array[0]); 
      colonna2.setText(array[1]); 
      colonna3.setText(array[2]); 
      colonna4.setText(array[3]); 
      colonna5.setText(array[4]); 
      colonna6.setText(array[5]); 
     } 
    } 


    public void setBackgroundColor (int colore) { 
     colonna1.setBackgroundColor(colore); 
     colonna2.setBackgroundColor(colore); 
     colonna3.setBackgroundColor(colore); 
     colonna4.setBackgroundColor(colore); 
     colonna5.setBackgroundColor(colore); 
     colonna6.setBackgroundColor(colore); 
    } 

    public void setForegroundColor (int colore) { 
     colonna1.setTextColor(colore); 
     colonna2.setTextColor(colore); 
     colonna3.setTextColor(colore); 
     colonna4.setTextColor(colore); 
     colonna5.setTextColor(colore); 
     colonna6.setTextColor(colore); 
    } 
} 

И в рамках деятельности я называю эти инструкции:

TableLayout tl = (TableLayout)findViewById(R.id.tableLayout); 
Riga rigaTitoli = new Riga (this); 
tl.addView(rigaTitoli); 

У меня есть это (рис # 3)

image

Где ошибка? Как я могу исправить?

+0

image # 2 - нужный результат? –

+0

Да, это должно быть так. – quasiaffatto

ответ

1
LinearLayout ll = (LinearLayout) LayoutInflater.from (getContext()).inflate(R.layout.custom_linearlayout , null); 

Вы пропусканием null в качестве родительского вида, что делает match_parent проклейки для вашего завышены LinearLayout, чтобы не работать. Подумайте о прохождении в качестве корня и удалите addView(), так как по умолчанию надуватель добавляет завышенный вид к непунктовому родительскому элементу.

+0

Это работает !! Большое спасибо! Мне нужно задать вопрос Solved, как я могу это сделать? – quasiaffatto

Смежные вопросы