用代码写的布局,但是tablelayout不能占满整个屏幕,不想用绝对布局去设置坐标,求助解决方法~谢谢。 10
packagesumakria.gumtree;importjava.util.ArrayList;importjava.util.Random;importandroi...
package sumakria.gumtree;
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class list_frame extends ViewGroup implements Globle {
private LinearLayout linear2;
private TextView text_current;
private ScrollView scrollView;
private TableLayout tableLayout;
private TableRow tableRow_current;
private ArrayList<TableRow> trList = new ArrayList<TableRow>();
private ArrayList<TextView> tvList = new ArrayList<TextView>();
// constructor
public list_frame(Context context) {
super(context);
this.init(context);
}
// initialization.
public void init(Context context) {
LinearLayout.LayoutParams linear1para = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linear2 = new LinearLayout(context);
linear2.setOrientation(LinearLayout.VERTICAL);
linear2.setLayoutParams(linear1para);
// scrollview configurations
scrollView = new ScrollView(context);
scrollView.setBackgroundColor(Color.YELLOW);
// tablelayout
tableLayout = new TableLayout(context);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(
480, LayoutParams.WRAP_CONTENT);
tableLayout.setLayoutParams(tableLayoutParams);
tableLayout.setStretchAllColumns(false);
tableLayout.setColumnStretchable(0, false);
// tablerows
LayoutParams tableRowParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tableRow_current = new TableRow(context);
tableRow_current.setLayoutParams(tableRowParams);
//转下篇
// set 20 rows with different texts and background colors.
for (int i = 0; i < 40; i++) {
tableRow_current = new TableRow(context);
tableRow_current.setLayoutParams(tableRowParams);
text_current = new TextView(context);
text_current.setTextSize(24.0f);
text_current.setText(new Integer(i).toString()
+ "\tlarge context!!!!!");
// generate random colors
int color;
Random rnd = new Random();
color = Color.argb(255, rnd.nextInt(255), rnd.nextInt(255),
rnd.nextInt(255));
if (i % 2 == 0) {
color = Color.DKGRAY;
}
else {
color = Color.BLACK;
}
tableRow_current.setBackgroundColor(color);
tvList.add(text_current);
tableRow_current.addView(text_current);
trList.add(tableRow_current);
tableLayout.addView(trList.get(i));
}
this.addView(scrollView);
scrollView.addView(linear2);
linear2.addView(tableLayout);
} 展开
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class list_frame extends ViewGroup implements Globle {
private LinearLayout linear2;
private TextView text_current;
private ScrollView scrollView;
private TableLayout tableLayout;
private TableRow tableRow_current;
private ArrayList<TableRow> trList = new ArrayList<TableRow>();
private ArrayList<TextView> tvList = new ArrayList<TextView>();
// constructor
public list_frame(Context context) {
super(context);
this.init(context);
}
// initialization.
public void init(Context context) {
LinearLayout.LayoutParams linear1para = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linear2 = new LinearLayout(context);
linear2.setOrientation(LinearLayout.VERTICAL);
linear2.setLayoutParams(linear1para);
// scrollview configurations
scrollView = new ScrollView(context);
scrollView.setBackgroundColor(Color.YELLOW);
// tablelayout
tableLayout = new TableLayout(context);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(
480, LayoutParams.WRAP_CONTENT);
tableLayout.setLayoutParams(tableLayoutParams);
tableLayout.setStretchAllColumns(false);
tableLayout.setColumnStretchable(0, false);
// tablerows
LayoutParams tableRowParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tableRow_current = new TableRow(context);
tableRow_current.setLayoutParams(tableRowParams);
//转下篇
// set 20 rows with different texts and background colors.
for (int i = 0; i < 40; i++) {
tableRow_current = new TableRow(context);
tableRow_current.setLayoutParams(tableRowParams);
text_current = new TextView(context);
text_current.setTextSize(24.0f);
text_current.setText(new Integer(i).toString()
+ "\tlarge context!!!!!");
// generate random colors
int color;
Random rnd = new Random();
color = Color.argb(255, rnd.nextInt(255), rnd.nextInt(255),
rnd.nextInt(255));
if (i % 2 == 0) {
color = Color.DKGRAY;
}
else {
color = Color.BLACK;
}
tableRow_current.setBackgroundColor(color);
tvList.add(text_current);
tableRow_current.addView(text_current);
trList.add(tableRow_current);
tableLayout.addView(trList.get(i));
}
this.addView(scrollView);
scrollView.addView(linear2);
linear2.addView(tableLayout);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询