Sto provando a creare dinamicamente oggetti TableRow
e aggiungerli a un TableLayout
. Gli oggetti TableRow
ha 2 elementi, un TextView
e un CheckBox
. Gli elementi TextView
devono avere il peso del layout impostato su 1 per Spingere gli elementi CheckBox
all'estrema destra.
Non riesco a trovare la documentazione su come impostare a livello di programmazione il peso del layout di un elemento TextView
.
Devi usare TableLayout.LayoutParams
con qualcosa del genere:
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
L'ultimo parametro è il peso.
La risposta è che devi usare TableRow.LayoutParams, non LinearLayout.LayoutParams o qualsiasi altro LayoutParams.
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
I diversi LayoutParam non sono intercambiabili e se si utilizza quello sbagliato, non sembra accadere nulla. Il genitore della vista testuale è una riga della tabella, quindi:
http://developer.Android.com/reference/Android/widget/TableRow.LayoutParams.html
Nelle risposte precedenti weight viene passato al costruttore di un nuovo oggetto SomeLayoutType.LayoutParams. Ancora in molti casi è più comodo usare gli oggetti esistenti - aiuta ad evitare di gestire parametri che non ci interessano.
Un esempio:
// Get our View (TextView or anything) object:
View v = findViewById(R.id.our_view);
// Get params:
LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) v.getLayoutParams();
// Set only target params:
loparams.height = 0;
loparams.weight = 1;
v.setLayoutParams(loparams);
TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);
1f è indicato come peso = 1; puoi dare 2f o 3f, le visualizzazioni si sposteranno accoding allo spazio
basta impostare i parametri di layout in quel layout come
crea una variabile param
Android.widget.LinearLayout.LayoutParams params = new Android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
1f è una variabile di peso
imposta il tuo widget o layout come
TextView text = (TextView) findViewById(R.id.text);
text.setLayoutParams(params);
TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
(O)
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
1f è riferito come peso = 1; in base alle tue necessità puoi dare 2f o 3f, le visualizzazioni si sposteranno accoding allo spazio. Per rendere la distanza specificata tra le viste nel layout Lineare, utilizzare weightsum per "LinearLayout".
LinearLayout ll_Outer= (LinearLayout ) view.findViewById(R.id.linearview);
LinearLayout llInner = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
llInner.Orientation = Orientation.Horizontal;
llInner.WeightSum = 2;
ll_Outer.AddView(llInner);
Puoi anche dare il peso separatamente come questo,
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lp1.weight=1;
Questo dovrebbe funzionare per te
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT LayoutParams.MATCH_PARENT);
param.weight=1.0f;
Questo lavoro per me, e spero che funzionerà anche per te
Imposta prima i LayoutParam per la vista genitore:
myTableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT));
quindi impostare per TextView (figlio):
TableLayout.LayoutParams textViewParam = new TableLayout.LayoutParams
(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT,1f);
//-- set components margins
textViewParam.setMargins(5, 0, 5,0);
myTextView.setLayoutParams(textViewParam);
C'è un altro modo per farlo. Nel caso in cui sia necessario impostare un solo parametro, ad esempio "altezza":
TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);