我是安卓新手。我有一个要求,我必须将 imageViews 动态添加到线性布局中,然后必须单独为每个图像设置动画。
我不知道将 imageViews 动态添加到线性布局的方法。请帮帮我。
谢谢
我是安卓新手。我有一个要求,我必须将 imageViews 动态添加到线性布局中,然后必须单独为每个图像设置动画。
我不知道将 imageViews 动态添加到线性布局的方法。请帮帮我。
谢谢
You can add view
dynamically like this.
LinearLayout.LayoutParams imParams =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imSex = new ImageView(context);
imSex.setImageResource(getmyImage());
mainlayout.addView(imSex,imParams);
看到这个
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.rainbow);
ImageView tv1 = new ImageView (this);
tv1.setImageresorce(R.drawable.image1);
ImageView tv2 = new ImageView (this);
tv2.setImageresorce(R.drawable.image2);
ImageView tv3 = new ImageView (this);
tv3.setGravity(Gravity.CENTER);
tv3.setImageresorce(R.drawable.image3);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tv3);
setContentView(ll);
}
看到这个有用的数据
http://mobile.tutsplus.com/tutorials/android/android-layout/
你可以试试这段代码。它还具有用于设置尺寸的位ImageView
以及多个 之间的边距ImageView's
。和int dimens = 45
是值并且int dimensMargin = 4;
正在pixel
被转换为 dp。
LinearLayout linlaLikes
必须在您的布局中,XML
然后在您的活动中投射。
LinearLayout linlaLikes = (LinearLayout) findViewById(R.id.linlaLikes);
ImageView imgUsers = new ImageView(getApplicationContext());
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 45;
float density = getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens = new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgUsers.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgUsers.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 4;
float densityMargin = getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin = new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
// SET YOUR IMAGER SOURCE TO YOUR NEW IMAGEVIEW HERE
// ADD THE NEW IMAGEVIEW WITH THE PROFILE PICTURE LOADED TO THE LINEARLAYOUT
linlaLikes.addView(imgUsers, imgvwMargin);
您可以为此http://developer.android.com/guide/components/fragments.html使用片段
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment imageFragment = new ExampleFragment();
fragmentTransaction.add(R.id.image_container, imageFragment);
fragmentTransaction.commit();