如何为我的imageview设置填充?

时间:2012-09-08 10:38:07

标签: android imageview padding

我创建了发光效果imageview应用程序。它的工作正常,我的问题是发光效果左,右,底部,顶部有点隐藏所以我试图设置填充但仍然我有同样的问题。如何解决这个问题?

代码:

ImageView mImageBk = (ImageView)itemTemplate.findViewById(R.id.imgBk);
RelativeLayout.LayoutParams ImgBKLayoutParams = new RelativeLayout.LayoutParams(scrWidth/8 + scrWidth/20, scrWidth/8 + scrWidth/20 );
ImgBKLayoutParams.leftMargin =0;
ImgBKLayoutParams.topMargin = 0;
mImageBk.setLayoutParams(ImgBKLayoutParams);
mImageBk.setImageResource(R.drawable.glow_effect);
mImageBk.setPadding(10, 10, 10, 10);
mImageBk.setVisibility(View.GONE);

完整代码:

public class tttAdapter extends BaseAdapter{
    private Context mContext;
    private int scrWidth=0;
    private int scrHeight=0;
    private int scrDpi = 0;
    private boolean setInvisible = false;
    private boolean visibleConf[];
    private boolean selected[];
    private boolean innerListView = false;


    private int[] IMAGE_IDS = {  
            R.drawable.s_record, R.drawable.s_record
    };  
    private String[] title = {
            "Tv", "My Favorites List1"
    };

    private String[] detail = {
            "Tv", "My Favorites List1"  
            };

    public favoritesAdapter(Context context) {
        mContext = context;
        visibleConf = new boolean[IMAGE_IDS.length];
        selected = new boolean[IMAGE_IDS.length];
    }

    public void updateImageSize(int width, int height, int dpi) {
        scrWidth= width;
        scrHeight = height;
        scrDpi = dpi;

    }
    public void setTextVisible(int position){
        if(IMAGE_IDS.length <= position)
            return;

        for(int i=0; i < IMAGE_IDS.length; i++) {
            visibleConf[i] = false;
        }

        visibleConf[position] = true;
    }
    public int getCount() {
        return IMAGE_IDS.length;
    }

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

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

    public void setTextInvisible(int position) {
        visibleConf[position] = false;
    }

    public void setInnerViewVisible(boolean enable) {
        innerListView = enable;
    }

    public void unsetSelectedItem(int position) {
        for(int i=0; i < IMAGE_IDS.length; i++) {
            selected[i] = false;
        }
    }

    public void setSelectedItem(int position) {
        for(int i=0; i < IMAGE_IDS.length; i++) {
            selected[i] = false;
        }

        selected[position] = true;
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemTemplate = inflater.inflate(R.layout.list_item, null);


        ImageView mImageBk = (ImageView)itemTemplate.findViewById(R.id.imgBk);
        RelativeLayout.LayoutParams ImgBKLayoutParams = new RelativeLayout.LayoutParams(scrWidth/8 + scrWidth/20, scrWidth/8 + scrWidth/20 );
        ImgBKLayoutParams.leftMargin =0;
        ImgBKLayoutParams.topMargin = 0;
        mImageBk.setLayoutParams(ImgBKLayoutParams);
        mImageBk.setImageResource(R.drawable.glow_effect);
        mImageBk.setPadding(10, 10, 10, 10);
        mImageBk.setVisibility(View.GONE);


        ImageView mImage = (ImageView)itemTemplate.findViewById(R.id.img);
        RelativeLayout.LayoutParams ImgLayoutParams = new RelativeLayout.LayoutParams(scrWidth/8 , scrWidth/8 );
        ImgLayoutParams.leftMargin = scrWidth/38;
        ImgLayoutParams.topMargin = scrWidth/38;

        mImage.setLayoutParams(ImgLayoutParams);
        mImage.setImageResource(IMAGE_IDS[position]);
        mImage.bringToFront();

        TextView mTextTitle = (TextView)itemTemplate.findViewById(R.id.title);
        mTextTitle.setClickable(false);
        mTextTitle.setEnabled(true);
        mTextTitle.setText(title[position]);

        TextView mTextDetail = (TextView)itemTemplate.findViewById(R.id.detail);
        mTextDetail.setClickable(false);
        mTextDetail.setEnabled(true);
        mTextDetail.setText(detail[position]);      

        if(selected[position] == true) {
            ImageView mArrowImg = (ImageView)itemTemplate.findViewById(R.id.imgArrow);
            //mArrowImg.setLayoutParams(new RelativeLayout.LayoutParams(scrWidth/32, scrHeight/32));
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(scrWidth/32, scrHeight/32);
            layoutParams.leftMargin = scrWidth / 8;
            layoutParams.topMargin = scrWidth / 16;
            mArrowImg.setLayoutParams(layoutParams);
            mArrowImg.setImageResource(R.drawable.arrow);           
            mImage.setBackgroundResource(R.drawable.test_glowb);
            mTextTitle.setVisibility(View.GONE);
            mTextDetail.setVisibility(View.GONE);
            mImageBk.setVisibility(View.VISIBLE);
        } else {
            if(innerListView == true) {
                mImage.setVisibility(View.GONE);
            }
        }

        if(visibleConf[position] == true) {
            mTextTitle.setTextSize((scrWidth*160) / (30 * scrDpi));
            mTextDetail.setTextSize((scrWidth*160) / (50 * scrDpi));                        
            mTextTitle.setTypeface(null,Typeface.BOLD);
            mTextDetail.setTypeface(null,Typeface.BOLD);
            mImageBk.setVisibility(View.VISIBLE);

        } else {

            mTextTitle.setTextSize((scrWidth*160) / (40 * scrDpi));
            mTextDetail.setTextSize((scrWidth*160) / (70 * scrDpi));
            //mTextTitle.setVisibility(View.GONE);
            //mTextDetail.setVisibility(View.GONE);         
            mImage.setAlpha(150);
        }

        if(innerListView == true) {
            mTextTitle.setVisibility(View.GONE);
            mTextDetail.setVisibility(View.GONE);
        }
        return itemTemplate;
    }
}

的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/thumb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

         <ImageView
            android:id="@+id/imgBk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="14dip"
        android:layout_marginTop="30dp"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/thumb"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumb"
            android:layout_toRightOf="@+id/thumb"
            android:layout_centerVertical="true"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/thumb"     
            android:layout_alignParentBottom="true"
            android:textColor="#ffffff" />

        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imgArrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>  
    </LinearLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

确保您为imageview小部件设置了边距/填充,而不是布局容器。

答案 1 :(得分:0)

我认为你想要边距而不是填充(对于imageView),因为填充在视图的内容中放置空格,意味着显示更少的空间。

边距不在视野范围内。