[英]Programmatically set the width and height of GridView And ImageView
我已經看到了有關如何分別設置ImageView和GridView的高度和寬度的問題。 這里 , 這里和這里的初學者
但是,我想知道如何設置兩者。
這是我的設置。 在我的GridView中,我有一個ImageView和一個TextView。 我有一些寬度和尺寸各不相同的圖像,我希望將其調整為始終為屏幕正方形尺寸的1/3。 在下面,我有一個文字說明。
這是到目前為止我所擁有的代碼...
gallerygridview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_light_blue"
android:orientation="vertical" >
<GridView
android:id="@+id/gridGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="3"
android:padding="0dp"
android:verticalSpacing="4dp" >
</GridView>
<ImageView
android:id="@+id/imgNoMedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="sans"
android:layout_below="@+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textColorHighlight="#000000"
android:gravity="center"
/>
</LinearLayout>
在我的活動中...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallerygridview);
GridView gridView = (GridView) findViewById(R.id.gridGallery);
m_adapter = new PhotoImageAdapter(getBaseContext(),m_images);
gridView.setAdapter(m_adapter); // uses the view to get the context instead of getActivity().
registerForContextMenu(gridView);
}
在我的Adapter類中...
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder=new Holder();
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.gallerygridview, parent, false);
holder.img = new ImageView(mContext);
holder.img = (ImageView) view.findViewById(R.id.imgNoMedia);
holder.tv = (TextView) view.findViewById(R.id.textView1);
int iDisplayWidth = mContext.getResources().getDisplayMetrics().widthPixels ;
int iImageWidth = (iDisplayWidth / 3)-15 ;
GridView.LayoutParams glp = (GridView.LayoutParams) view.getLayoutParams();
glp.width = iImageWidth;
glp.height = iImageWidth ;
view.setLayoutParams(glp);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
holder.tv.setText(m_images.get(position).key);
holder.img.setImageURI(Uri.parse(m_images.get(position).value));
return view;
}
現在,GridView恰好是屏幕大小的1/3。 並同時顯示圖像和文本。 但是我希望ImageView填充GridView單元內的剩余空間。
這就是我想要的...
但這就是它的樣子...
我究竟做錯了什么? 而且,如何以編程方式設置GridView和其內部的ImageView的高度?
提前致謝。
這是一個相對簡單的方法。 將GridView放入布局中,設置拉伸模式以拉伸列寬,將間距設置為0(或任意設置),並將列數設置為2:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
/>
</FrameLayout>
並參考此以了解有關GridView和ImageView的更多信息
看到您的輸出圖像后,這是我的理解以及出現此問題的原因
無論何時縮放圖像,都必須按照該圖像的縱橫比進行縮放。 例如:假設您的圖片尺寸為900 1200 (wh)
要將其寬度縮放到400
就需要這樣
newWidth = 400; newHeight = 1200 * (400/900) = 533.33
如果您希望imageview的圖像應自動縮放,請在xml布局中檢查ImageView的scaleType屬性( fitCenter,centerInside
等)
在您的getView中添加此行
holder.img.setScaleType(ScaleType.CENTER_INSIDE);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.