textview不会显示在图库项目中,并且每个项目的开头和结尾都有空格

时间:2015-11-24 13:47:36

标签: android android-gallery

我想将imageview和textview显示为图库中的Verticle项目,但不显示textview,并且在第一个项目和最后一个项目包含空格之前,如何删除此空间?

这是我的代码。

FilterActivity.java

public class FilterActivity extends Activity {

    ListView lstfilter;
    MyAdapter adapter;
    ImageView next,prev;
    Gallery horizontalscrollview;
    public static ArrayList<String> mylist;
    public static ArrayList<String> mycategorylist;
    HorizontalScrollView horizontalScroll;
    HorizontalScrollAdapter horizonAdapter;
    LinearLayout hlayout;
    ArrayList<Integer> array_image = new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        mylist = new ArrayList<String>();
        mycategorylist = new ArrayList<String>();
        horizontalscrollview = (Gallery) findViewById(R.id.horizontalscrollview);
        horizontalscrollview.setHorizontalFadingEdgeEnabled(false);
        hlayout = (LinearLayout)findViewById(R.id.horizontal_menu);
        prev = (ImageView)findViewById(R.id.imgprev);
        next = (ImageView) findViewById(R.id.imgnext);
        lstfilter = (ListView) findViewById(R.id.lstfilter);
        mylist.add("Rs.250 n Below(253 result found)");
        mylist.add("Rs.251 - 500(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mylist.add("Rs.501 - 750(25301 result found))");
        mycategorylist.add("Brand");
        mycategorylist.add("Price");
        mycategorylist.add("Discount");
        mycategorylist.add("Availability");
        mycategorylist.add("More");

        array_image.add(R.drawable.img_brand);
        array_image.add(R.drawable.img_rupee);
        array_image.add(R.drawable.img_discount);
        array_image.add(R.drawable.yellow_check_mark);
        array_image.add(R.drawable.yellow_check_mark);

        horizonAdapter = new HorizontalScrollAdapter(this, mycategorylist,array_image);
        horizontalscrollview.setAdapter(horizonAdapter);


        // Getting adapter by passing xml data ArrayList
        prev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                horizontalscrollview = (Gallery) findViewById(R.id.horizontalscrollview);
                    int firstVisibleItem = horizontalscrollview.getFirstVisiblePosition();
                    int lastVisibleCount = horizontalscrollview.getLastVisiblePosition();
                    if (firstVisibleItem != 0) {
                        int count1 = firstVisibleItem
                                - (lastVisibleCount - firstVisibleItem);
                        if (count1 < 0) {
                            horizontalscrollview.setSelection(0);

                        }

                        else {
                            horizontalscrollview.setSelection(count1);

                        }
                        Log.d("item selected:",""+count1);
                        if(count1 == 0)
                        {
                            Log.d("first item visible","");
                            prev.setVisibility(ImageView.GONE);
                            next.setVisibility(ImageView.VISIBLE);

                        }
                        else
                        {
                            prev.setVisibility(ImageView.VISIBLE);
                            next.setVisibility(ImageView.GONE);
                        }
                    }


                }
        });

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//              horizontalScroll.scrollTo(
//                      (int) horizontalScroll.getScrollX() + 10,
//                      (int) horizontalScroll.getScrollY());

                horizontalscrollview = (Gallery) findViewById(R.id.horizontalscrollview);
                    int count = horizontalscrollview.getCount();
                    int firstVisibleItem = horizontalscrollview.getFirstVisiblePosition();
                    int lastVisibleCount = horizontalscrollview.getLastVisiblePosition();
                    if (lastVisibleCount != count) {
                        int count1 = lastVisibleCount
                                + (lastVisibleCount - firstVisibleItem);
                        if (count1 > count) {
                            horizontalscrollview.setSelection(count);

                        } else {
                            horizontalscrollview.setSelection(count1);

                        }
                        Log.d("item selected:",""+lastVisibleCount);
                        if(count1 == lastVisibleCount)
                        {
                            Log.d("last item visible","");
                            prev.setVisibility(ImageView.VISIBLE);
                            next.setVisibility(ImageView.GONE);

                        }
                        else
                        {
                            prev.setVisibility(ImageView.VISIBLE);
                            next.setVisibility(ImageView.GONE);
                        }
                    }

                }

        });
        adapter = new MyAdapter(this, mylist);
        lstfilter.setAdapter(adapter);

    }

activity_filter.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="#0088cc"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.10" />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:background="@null"
            android:scaleType="fitCenter" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25" />

        <ImageButton
            android:id="@+id/imgDeliveryBack"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.28"
            android:background="@null"
            android:padding="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/img_back_arrow" />

        <TextView
            android:id="@+id/txvdeliveryheader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            android:fontFamily="Roboto regular"
            android:gravity="left|center"
            android:text="Filter"
            android:textColor="#f8f8f8"
            android:textSize="18sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".9"
            android:gravity="left|center"
            android:text="@null"
            android:textColor="#ffffff" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgprev"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
            android:background="#3374a3"
            android:src="@drawable/img_back_arrow" />

        <LinearLayout
            android:id="@+id/horizontal_menu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:background="#168fcc"
            android:orientation="horizontal" >

            <Gallery
                android:id="@+id/horizontalscrollview"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.7"
                android:background="@drawable/selected"
                android:columnWidth="70dp"
                android:fadingEdgeLength="0dp"
                android:horizontalSpacing="5dp"
                android:stretchMode="columnWidth"
                android:verticalSpacing="5dp" />
        </LinearLayout>

        <ImageView
            android:id="@+id/imgnext"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
            android:background="#3374a3"
            android:src="@drawable/img_next" />
    </LinearLayout>

    <ListView
        android:id="@+id/lstfilter"
        android:layout_width="match_parent"
        android:layout_height="340dp"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="40dp"
        android:layout_weight="0.3" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="0.5" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#035f8d" >

                <TextView
                    android:id="@+id/txvremove"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:padding="5dp"
                    android:src="@drawable/icon_trash_white"
                    android:text="@string/clear_filters"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="0.5" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="#000000" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:padding="5dp"
                    android:src="@drawable/icon_trash_white"
                    android:text="APPLY"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

activity_horizo​​ntal_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#168fcc"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imgcategory"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/txvcategoryname"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <View
            android:layout_width="1dip"
            android:layout_height="match_parent"
            android:background="#066da1" />
    </LinearLayout>

</LinearLayout>

Horizo​​ntalScrollAdapter.java

public class HorizontalScrollAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<String> data;
    ArrayList<Integer> image;
    private static LayoutInflater inflater = null;

    // public ImageLoader imageLoader;

    public HorizontalScrollAdapter(Activity a, ArrayList<String> list,ArrayList<Integer> image_array) {
        activity = a;
        data = list;
        image = image_array;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.activity_horizontal_item, null);

        TextView txvfirst = (TextView) vi.findViewById(R.id.txvcategoryname); // title
        ImageView imgcategory = (ImageView) vi.findViewById(R.id.imgcategory);

        // HashMap<String, String> song = new HashMap<String, String>();
        // song = data.get(position);

        // Setting all values in listview
        // title.setText(song.get(CustomizedListView.KEY_TITLE));
        // artist.setText(song.get(CustomizedListView.KEY_ARTIST));
        // duration.setText(song.get(CustomizedListView.KEY_DURATION));
        // imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL),
        // thumb_image);
        txvfirst.setText(data.get(position));
        imgcategory.setImageResource(image.get(position));
        return vi;
    }

如何在图库的每个图像下方显示文字,我还想删除每个图库项目前后的空间。

1 个答案:

答案 0 :(得分:0)

实际上在activity_filter.xml布局中,Gallery小部件的父布局(此处为LinearLayout)具有40dp的固定高度,如果增加它或使用权重处理它,它将显示{ {1}}。你可以看到,我把这个高度增加到TextView,它清楚地显示了100dp。这个空间即将到来,因为你已经TextView作为horizontalSpacing,如果你不想要列之间的空格,那就试着减少它。

<强>更新 -

我已更新我的答案以删除5dp周围的空格,此处我Gallery,现在RelativeLayoutPrev按钮与图库重叠。希望它能解决你的问题。

Next