【问题标题】:Android image view gravityAndroid图像视图重力
【发布时间】:2011-11-29 17:25:21
【问题描述】:

如何动态设置重力?

Bitmap bmp;
im = new ImageView (this);
bmp=getbmp(img);
im.setImageBitmap(bmp);  

现在我想把图片放在顶部。我可以在 main.xml 中没有 android:gravity 吗?

编辑:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical"

    >
</ScrollView>

我有一个框架布局,我添加了图像,然后是文本,我希望图像位于屏幕顶部。

FrameLayout fm = new FrameLayout (this);
fm.addView(im);
fm.addView(fin); // fin = the text view
setContentView(fm);

【问题讨论】:

  • 更多信息,请。发布您的布局文件。
  • 有什么理由不直接把ImageView放到布局文件里?
  • 好吧,如果我将图像视图放在布局文件中,在 ScrollView 内部,应用程序崩溃,原因是所选子级已经有父级。其次,我有一个文本视图和一个图像视图,所以我想动态创建它们。
  • 是的,您应该将 ImageView 放在 ScrollView 的子项中。一个 ScrollView 只能有一个孩子 - 即一个 LinearLayout 或 RelativeLayout。这应该不是问题。
  • 好的,所以你说要在 ScrollView 和 LinearLayout 内创建一个 LinearLayout 来放置文本和图像视图?

标签: android image view gravity


【解决方案1】:

使用ImageView.setScaleType()ImageView.ScaleType.FIT_START 作为值。

【讨论】:

  • 对不起,是这样的:im.setScaleType(ImageView.ScaleType.FIT_START);
  • 谢谢,它可以工作,但为什么它把文字放在图像上?我能做什么?
  • 代码:FrameLayout fm = new FrameLayout (this); fm.addView(im); fm.addView(fin); setContentView(fm); fintextView
【解决方案2】:

试试这个:

Bitmap bmp;    
ImageView img= new ImageView (this);       
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.icon);      
img.setImageBitmap(bmp);             
img.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,                       
                  LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
mLinearLayout.addView(img);

请注意,你需要在FrameLayout.LayoutParams(width,height,gravity)中根据你要设置的重力来设置imageview的宽高。

【讨论】:

    【解决方案3】:

    请参阅“http://developer.android.com/reference/android/widget/ImageView.ScaleType.html”。

    【讨论】:

      【解决方案4】:

      如果对你有帮助,你可以试试这个代码 xml文件

      <GridView android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:id="@+id/gallerylistactivity_gallery"
          android:columnWidth="90dp" android:numColumns="4"
          android:verticalSpacing="5dp" android:horizontalSpacing="10dp"
          android:gravity="center" android:stretchMode="spacingWidth"/>
      

      .java 文件

      公共类 GalleryListAdapter 扩展 BaseAdapter{

      private Context mContext;
      
      private Integer[] mImageIds = { R.drawable.gallery_01,
              R.drawable.gallery_02, R.drawable.gallery_03,
              R.drawable.gallery_04, R.drawable.gallery_05,
              R.drawable.gallery_06, R.drawable.gallery_07,
              R.drawable.gallery_08, R.drawable.gallery_10,
              R.drawable.gallery_09 };
      
      public GalleryListAdapter(Context c) {
          this.mContext = c;
      
      }
      
      @Override
      public int getCount() {
          return mImageIds.length;
      
      }
      
      @Override
      public Object getItem(int position) {
          return position;
      }
      
      @Override
      public long getItemId(int position) {
          return position;
      }
      
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
      
          ImageView imageView = new ImageView(mContext);
          imageView.setImageResource(mImageIds[position]);
          imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          imageView.setPadding(3, 3, 3, 3);
      
          return imageView;
      
      }
      

      【讨论】:

        【解决方案5】:

        您可以将 ImageView 放在一个 LinearLayout 中,并使用 LinearLayout 的 setLayoutParams 来设置布局的重力。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多