java - 如何在 Android 中增加 ImageView 的大小

标签 java android android-xml android-gallery galleryview

我正在从事一个项目,该项目要求我实现两个水平 ScrollView ,并在它们之间放置一个 ImageView 。我想扩展 ImageView 的大小以填补 ScrollView 之间的空白。我浏览了示例代码,但它似乎没有提到 ImageView 大小的任何地方。我在描述我的问题的图像之后附上了下面的代码。

enter image description here

public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;

private Integer[] Imgid = {
    R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery1 = (Gallery)findViewById(R.id.examplegallery1);
gallery1.setAdapter(new AddImgAdp(this));

imgView = (ImageView)findViewById(R.id.ImageView01);    
imgView.setImageResource(Imgid[0]);


 gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        imgView.setImageResource(Imgid[position]); 
    }
});

}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg =  typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
}

public int getCount() {
    return Imgid.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
}
}

这是我的 XML 主文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

下面是我的属性文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GalleryTheme">
    <attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>

最佳答案

维纳,

这里的问题实际上是两个问题:

  1. ImageView 的大小基于父级,这意味着它共享其宽度高度。

  2. 图像会调整大小以匹配最受限制的尺寸(宽度)。

处理此问题的最简单方法是设置 ImageView 的 ScaleType。有多种缩放类型,但您可能想要的是 Center Crop。在代码中,这是由 view_name.setScaleType(ImageView.ScaleType.CENTER_CROP) 完成的。您也可以使用属性 android:scaleType="centerCrop" 为您的 ImageView 在 XML 中执行此操作。请注意,这将调整您的图像大小并保持您的纵横比,但它会切断边。

只需将上述属性添加到您的 XML 中的 ImageView(可能在您的 layout_weight 下),它就会执行您想要的操作。

希望对您有所帮助,

模糊逻辑

关于java - 如何在 Android 中增加 ImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891909/

相关文章:

java - svnkit:如何枚举根目录中的svn存储库?

java - 如何在 Jackson 自定义 JSON 反序列化器中使用常规 JSON 解释

java - 有没有办法使用 Eclipse 列出某个类的 equals() 的所有调用?

android - 缺少 cordova.js 和未捕获的 ReferenceError : Media is not defined

android - Activity 动画从底部滑动

android - 异构网格布局

java - 在客户端/Web 服务器架构中实现数字签名

android - Android 中的 ImageView 内容是否有 tools 属性?

android - Android 中的自定义 EditText 边框

android - CoordinatorLayout 内具有 FloatingActionButton 的 BottomNavigationView 与位置不匹配