我有一个看似无法正确调整大小的页脚图像:它在每一侧都被剪切掉,并且看起来与其他图像重叠(也分配给PagerAdapter)。
如何避免这种情况?
截图:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.classical_up_btn,
R.drawable.country_up_btn,
R.drawable.dance_up_btn,
R.drawable.hiphop_up_btn
};
public int getCount() {
return mImages.length;
}
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_drawer"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/darkgrey"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAFFFFFF" >
<ScrollView
android:id="@+id/groupScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Throw 'Em Up"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn1"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn1"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by DJ Generic |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview1"
android:layout_width="match_parent"
android:layout_height="200dip"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bulls On Parade"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn1"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by Rage Against The Machine |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview2"
android:layout_width="match_parent"
android:layout_height="200dip"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Isaac Daniel on CNN with Anderson Cooper"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn1"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by idconex |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView2a"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview3"
android:layout_width="match_parent"
android:layout_height="200dip"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bulls On Parade"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn1"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by Rage Against The Machine |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="25dp" >
</View>
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtubethumbnailview4"
android:layout_width="match_parent"
android:layout_height="200dip"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Isaac Daniel on CNN with Anderson Cooper"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/fav_up_btn"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1a"
android:background="@drawable/fav_up_btn1"
android:gravity="right"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textView3a"
android:gravity="left"
android:text="by idconex |"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/lightgrey" />
<TextView
android:id="@+id/textView3a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text=" 100,000 views"
android:textAppearance="?android:attr/textAppearanceSmall" />
<View
android:layout_width="1dp"
android:layout_height="75dp" >
</View>
</LinearLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:layout_alignParentBottom="true"
android:background="@color/darkgrey" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="@drawable/home_up_btn" />
<HorizontalScrollView
android:id="@+id/groupScrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/darkgrey" >
<ImageButton
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="@color/darkgrey"
android:src="@drawable/selstation_up_btn" />
</HorizontalScrollView>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/scroll_lt_arrow" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/scroll_rt_arrow" />
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
如果您使用CENTER,FIT_XY或任何其他类似参数,则需要为View指定精确大小,而不是使用match_parent
,fill_parent
等。如果您确实要使用在父布局参数中,您需要使用
Drawable d = getResources().getDrawable(drawable_id);
imageView.setImageBitmap(((BitmapDrawable) d).getBitmap());