谁能告诉我怎么设置不了ImageView的setScaleType的属性

Lixsck 2014-06-24 04:03:40
package com.example.android_tablelayout;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

public class MainActivity extends Activity {
Integer[] image = {

R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3,
R.drawable.guide_4, R.drawable.guide_1, R.drawable.guide_2,
R.drawable.guide_3, R.drawable.guide_1, R.drawable.guide_2,
R.drawable.guide_3,

};
private ImageView imageview;
TableRow tab1, tab2, tab3, tab4;
TableLayout tablelayout;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tablelayout = (TableLayout) findViewById(R.id.tab_layout);
tab1 = new TableRow(this);
tab2 = new TableRow(this);
tab3 = new TableRow(this);
tab4 = new TableRow(this);
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels;
int height = metric.heightPixels;
int j = 1;
for (int i = 0; i < image.length; i++) {
imageview = new ImageView(this);
TableRow.LayoutParams layout = new TableRow.LayoutParams((height) / 4, (height - 100) / 4);
layout.setMargins(5, 5, 5, 5);
imageview.setScaleType(ImageView.ScaleType.CENTER);
imageview.setLayoutParams(layout);
imageview.setBackgroundResource(image[i]);
final int k = i;
imageview.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"您点击了图片"+k,Toast.LENGTH_SHORT).show();
intent = new Intent();
intent.setClass(MainActivity.this, CompanyStyleInfo.class);
startActivity(intent);
}
});
if (j == 1) {
tab1.addView(imageview);
System.out.println("tab1");
} else if (j == 2) {
tab2.addView(imageview);
System.out.println("tab2");
} else if (j == 3) {
tab3.addView(imageview);
System.out.println("tab3");
} else if (j == 4) {
tab4.addView(imageview);
System.out.println("tab4");

}
j++;
if (j == 5) {
j = 1;
}
}

// imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
tablelayout.addView(tab1);
tablelayout.addView(tab2);
tablelayout.addView(tab3);
tablelayout.addView(tab4);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
...全文
354 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
董小帅 2016-10-11
  • 打赏
  • 举报
回复
3L正解 。
氢电公敌 2015-11-22
  • 打赏
  • 举报
回复
ScaleType 只能应用于src不能应用于background,所以imageview.setImageResource(相当于xml中的src属性)之后scaleType属性就可以使用了
Lixsck 2014-06-24
  • 打赏
  • 举报
回复
具体怎么写啊
  • 打赏
  • 举报
回复
imageview.setBackgroundResource => imageview.setImageResource

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧