Java 在表视图中添加布局
我用xml创建了一个代码,使用tablerow创建了一个tablelayou,每个tablerow有3个线性布局。 第一条线性布局的宽度为40dp,高度为90dp。第二个线性布局的宽度为0dp,高度为90dp,重量为1,第三个线性布局等于de first。所以我有一个结果: 所以我需要在java上创建相同的布局,我使用相同的配置,但是对齐不好。我怎样才能找到它呢?看: 我用边框表示对齐。这是java代码:Java 在表视图中添加布局,java,android,Java,Android,我用xml创建了一个代码,使用tablerow创建了一个tablelayou,每个tablerow有3个线性布局。 第一条线性布局的宽度为40dp,高度为90dp。第二个线性布局的宽度为0dp,高度为90dp,重量为1,第三个线性布局等于de first。所以我有一个结果: 所以我需要在java上创建相同的布局,我使用相同的配置,但是对齐不好。我怎样才能找到它呢?看: 我用边框表示对齐。这是java代码: int wlienar = (int) TypedValue.applyDimens
int wlienar = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
int hlienar = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
int wLienarUserDataBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics());
int hLienarUserDataBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
int wUserPicture = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
int hUserPicture = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
int wLogoBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
int hLogoBeer = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
int margim = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
Bitmap bmImage = BitmapFactory.decodeResource(getResources(),R.mipmap.eu);
iconViewRound.setImageBitmap(bmIcon);
imageViewRound.setImageBitmap(bmImage);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.feeds_table);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); //white background
border.setStroke(1, 0xFF000000); //black border with full opacity
for(int i=0 ; i < 10 ; i++) {
/*create a linear layout to user picture*/
LinearLayout linearUserPicture = new LinearLayout(this);
linearUserPicture.setOrientation(LinearLayout.VERTICAL);
linearUserPicture.setBackground(border);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wlienar, hlienar);
layoutParams.setMargins(0, 0, margim, 0);
// layoutParams.gravity = Gravity.NO_GRAVITY;
RoundedImageView imageView = new RoundedImageView(this, null);
imageView.setImageResource(R.mipmap.eu);
//setting image position
imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(wUserPicture, hUserPicture));
linearUserPicture.addView(imageView, layoutParams);
/*create a linear layout to user comments beer*/
//user name
TextView textViewUserName = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewUserName.setTextAppearance(this, R.style.feeds_user_connected_style);
} else {
textViewUserName.setTextAppearance(R.style.feeds_user_connected_style);
}
textViewUserName.setText(R.string.user_feed_name);
// beer in date
TextView textViewDateBeerIn = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewDateBeerIn.setTextAppearance(this, R.style.dateBeeinAndPlace);
} else {
textViewDateBeerIn.setTextAppearance(R.style.dateBeeinAndPlace);
}
textViewDateBeerIn.setText(R.string.date_beerin);
// textview beer
TextView textViewBeer = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewBeer.setTextAppearance(this, R.style.beerBeerin);
} else {
textViewBeer.setTextAppearance(R.style.beerBeerin);
}
textViewBeer.setText(R.string.beerBeerin);
//textview place beer in
TextView textViewPlace = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewPlace.setTextAppearance(this, R.style.dateBeeinAndPlace);
} else {
textViewPlace.setTextAppearance(R.style.dateBeeinAndPlace);
}
textViewPlace.setText(R.string.place_beerin);
//textview place count comments
TextView textViewCountComments = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewCountComments.setTextAppearance(this, R.style.countBeerinComment);
} else {
textViewCountComments.setTextAppearance(R.style.countBeerinComment);
}
textViewCountComments.setText(R.string.countBeerinComment);
LinearLayout lienarComponents = new LinearLayout(this);
lienarComponents.setOrientation(LinearLayout.VERTICAL);
lienarComponents.addView(textViewUserName);
lienarComponents.addView(textViewDateBeerIn);
lienarComponents.addView(textViewBeer);
lienarComponents.addView(textViewPlace);
lienarComponents.addView(textViewCountComments);
LinearLayout linearUserDataBeer = new LinearLayout(this);
linearUserDataBeer.setOrientation(LinearLayout.VERTICAL);
linearUserDataBeer.setBackground(border);
LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(0, hLienarUserDataBeer,1);
linearUserDataBeer.addView(lienarComponents, layoutParamsUserDataBeer);
//logo beer
/*create a linear layout to logo beer*/
LinearLayout linearLogoBeer = new LinearLayout(this);
linearLogoBeer.setOrientation(LinearLayout.HORIZONTAL);
linearLogoBeer.setBackground(border);
LinearLayout.LayoutParams layoutParamsLogo = new LinearLayout.LayoutParams(wlienar, hlienar);
//ImageView Setup
ImageView imageViewLogoBeer = new ImageView(this);
imageViewLogoBeer.setLayoutParams(
new ViewGroup.LayoutParams(
// or ViewGroup.LayoutParams.WRAP_CONTENT
wLogoBeer,
// or ViewGroup.LayoutParams.WRAP_CONTENT,
hLogoBeer));
imageViewLogoBeer.setImageResource(R.mipmap.beerin);
linearLogoBeer.addView(imageViewLogoBeer, layoutParamsLogo);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
//tr.setBackgroundResource(R.drawable.border);
tr.setBackground(border);
tr.addView(linearUserPicture);
tr.addView(linearUserDataBeer);
tr.addView(linearLogoBeer);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}
intWLIENAR=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,getResources().getDisplayMetrics());
int hlienar=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,90,getResources().getDisplayMetrics());
int wLienarUserDataBeer=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,0,getResources().getDisplayMetrics());
int hLienarUserDataBeer=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,90,getResources().getDisplayMetrics());
int wUserPicture=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,getResources().getDisplayMetrics());
int hUserPicture=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,getResources().getDisplayMetrics());
int-wLogoBeer=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,getResources().getDisplayMetrics());
int hLogoBeer=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,getResources().getDisplayMetrics());
int margim=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5,getResources().getDisplayMetrics());
位图bmImage=BitmapFactory.decodeResource(getResources(),R.mipmap.eu);
设置图像位图(bmIcon);
设置图像位图(bmImage);
/*查找main.xml中定义的Tablelayout*/
TableLayout tl=(TableLayout)findviewbyd(R.id.feeds\u table);
GradientDrawable边框=新的GradientDrawable();
border.setColor(0xFFFFFFFF)//白色背景
设置行程(1,0xFF000000)//完全不透明的黑色边框
对于(int i=0;i<10;i++){
/*为用户图片创建线性布局*/
LinearLayout linearUserPicture=新的LinearLayout(本);
linearUserPicture.setOrientation(LinearLayout.VERTICAL);
线性地形图.退坡地(边界);
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(wlienar,hlienar);
layoutParams.setMargins(0,0,margim,0);
//layoutParams.gravity=重力。无重力;
RoundedImageView imageView=新的RoundedImageView(该值为空);
setImageResource(R.mipmap.eu);
//设置图像位置
imageView.setLayoutParams(新的ViewGroup.MarginLayoutParams(Wuser图片,hUserPicture));
linearUserPicture.addView(图像视图,布局参数);
/*创建线性布局以显示用户评论*/
//用户名
TextView textViewUserName=新的TextView(此);
如果(Build.VERSION.SDK_INT<23){
textViewUserName.setTextAppearance(这个,R.style.feeds\u user\u connected\u样式);
}否则{
textViewUserName.setTextAppearance(R.style.feeds\u user\u connected\u样式);
}
textViewUserName.setText(R.string.user\u feed\u name);
//枣啤酒
TextView textViewDateBeerIn=新的TextView(此);
如果(Build.VERSION.SDK_INT<23){
textViewDateBeerIn.setTextAppearance(此为R.style.DateBeeindPlace);
}否则{
textViewDateBeerIn.setTextAppearance(R.style.DateBeeindPlace);
}
textViewDateBeerIn.setText(R.string.date\u beerin);
//文本视图啤酒
TextView textViewBeer=新的TextView(此);
如果(Build.VERSION.SDK_INT<23){
textViewBeer.setTextAppearance(这个,R.style.beerBeerin);
}否则{
textViewBeer.setTextAppearance(R.style.beerBeerin);
}
textViewBeer.setText(R.string.beerBeerin);
//文本视图将啤酒放入
TextView textViewPlace=新的TextView(此);
如果(Build.VERSION.SDK_INT<23){
textViewPlace.setTextAppearance(此为R.style.DateBeeindPlace);
}否则{
textViewPlace.setTextAppearance(R.style.DateBeeindPlace);
}
textViewPlace.setText(R.string.place\u beerin);
//textview地点计数注释
TextView textViewCountComments=新的TextView(此);
如果(Build.VERSION.SDK_INT<23){
textViewCountComments.setTextAppearance(this,R.style.countBeerinComment);
}否则{
textViewCountComments.setTextAppearance(R.style.countBeerinComment);
}
textViewCountComments.setText(R.string.countBeerinComment);
LinearLayout lienarComponents=新的LinearLayout(本);
lienarComponents.setOrientation(线性布局.垂直);
lienarComponents.addView(textViewUserName);
lienarComponents.addView(textViewDateBeerIn);
lienarComponents.addView(textViewBeer);
lienarComponents.addView(textViewPlace);
lienarComponents.addView(textViewCountComments);
LinearLayout linearUserDataBeer=新的LinearLayout(本);
linearUserDataBeer.设置方向(LinearLayout.垂直);
线性回归(边界);
LinearLayout.LayoutParams layoutParamsUserDataBeer=新的LinearLayout.LayoutParams(0,hLienarUserDataBeer,1);
linearUserDataBeer.addView(lienarComponents、layoutParamsUserDataBeer);
//标志啤酒
/*创建一个线性布局来标识啤酒*/
LinearLayout linearLogoBeer=新的LinearLayout(本);
linearLogoBeer.设置方向(LinearLayout.水平);
线性回归背景(边界);
LinearLayout.LayoutParams layoutParamsLogo=新的LinearLayout.LayoutParams(wlienar,hlienar);
//ImageView设置
ImageView imageViewLogoBeer=新的ImageView(此);
imageViewLogoBeer.se
TextView myTextView = new TextView(context, attributeSet);
LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(0, hLienarUserDataBeer,1);
LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(ViewGroup.MATCH_PARENT, hLienarUserDataBeer,0);
tl.setColumnStretchable( 1, true );
TableLayout tl = (TableLayout) findViewById(R.id.feeds_table);
tl.setColumnStretchable( 1, true ); // this line fix the error
for(int i=0 ; i < 10 ; i++) {
/*create a linear layout to user picture*/
LinearLayout linearUserPicture = new LinearLayout(this);
linearUserPicture.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wlienar, hlienar);
layoutParams.setMargins(0, 0, margim, 0);
RoundedImageView imageView = new RoundedImageView(this, null);
imageView.setImageResource(R.mipmap.eu);
//setting image position
imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(wUserPicture, hUserPicture));
linearUserPicture.addView(imageView, layoutParams);
/*create a linear layout to user comments beer*/
//user name
TextView textViewUserName = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewUserName.setTextAppearance(this, R.style.feeds_user_connected_style);
} else {
textViewUserName.setTextAppearance(R.style.feeds_user_connected_style);
}
textViewUserName.setText(R.string.user_feed_name);
// beer in date
TextView textViewDateBeerIn = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewDateBeerIn.setTextAppearance(this, R.style.dateBeeinAndPlace);
} else {
textViewDateBeerIn.setTextAppearance(R.style.dateBeeinAndPlace);
}
textViewDateBeerIn.setText(R.string.date_beerin);
// textview beer
TextView textViewBeer = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewBeer.setTextAppearance(this, R.style.beerBeerin);
} else {
textViewBeer.setTextAppearance(R.style.beerBeerin);
}
textViewBeer.setText(R.string.beerBeerin);
//textview place beer in
TextView textViewPlace = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewPlace.setTextAppearance(this, R.style.dateBeeinAndPlace);
} else {
textViewPlace.setTextAppearance(R.style.dateBeeinAndPlace);
}
textViewPlace.setText(R.string.place_beerin);
//textview place count comments
TextView textViewCountComments = new TextView(this);
if (Build.VERSION.SDK_INT < 23) {
textViewCountComments.setTextAppearance(this, R.style.countBeerinComment);
} else {
textViewCountComments.setTextAppearance(R.style.countBeerinComment);
}
textViewCountComments.setText(R.string.countBeerinComment);
//adicionado os camponentes no linear que irá ajuntar todos no linear principal
LinearLayout lienarComponents = new LinearLayout(this);
lienarComponents.setOrientation(LinearLayout.VERTICAL);
lienarComponents.addView(textViewUserName);
lienarComponents.addView(textViewDateBeerIn);
lienarComponents.addView(textViewBeer);
lienarComponents.addView(textViewPlace);
lienarComponents.addView(textViewCountComments);
LinearLayout linearUserDataBeer = new LinearLayout(this);
linearUserDataBeer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParamsUserDataBeer = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, hLienarUserDataBeer);
linearUserDataBeer.addView(lienarComponents, layoutParamsUserDataBeer);
//logo beer
/*create a linear layout to logo beer*/
LinearLayout linearLogoBeer = new LinearLayout(this);
linearLogoBeer.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParamsLogo = new LinearLayout.LayoutParams(wlienar, hlienar);
//ImageView Setup
ImageView imageViewLogoBeer = new ImageView(this);
imageViewLogoBeer.setLayoutParams(
new ViewGroup.LayoutParams(
// or ViewGroup.LayoutParams.WRAP_CONTENT
wLogoBeer,
// or ViewGroup.LayoutParams.WRAP_CONTENT,
hLogoBeer));
imageViewLogoBeer.setImageResource(R.mipmap.beerin);
linearLogoBeer.addView(imageViewLogoBeer, layoutParamsLogo);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setBackgroundResource(R.drawable.border);
tr.addView(linearUserPicture);
tr.addView(linearUserDataBeer);
tr.addView(linearLogoBeer);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}