android listview addHeaderView中心

时间:2015-05-25 10:00:38

标签: android android-listview

我有listview,我将headerview添加到listview。但它不是中心,我不想创建布局标题视图。你能帮助我吗? 这是代码:

  //add header view
        final ImageView headerView = new ImageView(getActivity());
        headerView.setLayoutParams(new AbsListView.LayoutParams((int) CommonUtil.convertDpToPixel(151,
                                                                                                  getActivity()),
                                                                (int) CommonUtil.convertDpToPixel(20,
                                                                                                  getActivity())));
       headerView.setPadding(0,
                             10,
                             0,
                             10);
        headerView.setBackgroundResource(R.drawable.ic_powered_by_foursquare);

        mLvVenueTips.addHeaderView(headerView);

它是主要的xml:

<?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"
             android:orientation="vertical">

    <ImageView
        android:id="@+id/img_venue_tips_background"
        style="@style/CreateTrips.Widget.BackgroundImage"
        android:contentDescription="@null"/>

    <!-- The Action Bar -->
    <com.createtrips.ui.widgets.ActionBar
        android:id="@+id/ab_venue_tips"
        style="@style/CreateTrips.Widget.ActionBar"
        android:layout_alignParentTop="true"/>

    <ListView
        android:id="@+id/lv_venue_tips"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/widget_action_bar_height"
        android:divider="@color/transparent_white_percent_10"
        android:layout_gravity="center|top"
        android:dividerHeight="@dimen/widget_divider_height"/>
</FrameLayout>

是Model xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="20dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingBottom="20dp"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_tips_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:textColor="@color/white"
        android:text="@string/text_drunken_translate"
        android:textSize="@dimen/text_light_medium"/>

    <TextView
        android:id="@+id/tv_name_user_send_and_date_tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_tips_message"
        android:text="@string/text_name_of_the_user_send_the_tip_and_date_format"
        android:textColor="@color/text_secondary"
        android:textSize="@dimen/text_extra_small"/>


    <Button
        android:id="@+id/btn_drunken_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_tips_message"
        android:background="@color/transparent"
        android:text="@string/text_drunken_translate"
        android:textColor="@color/text_primary"
        android:textSize="@dimen/text_extra_small"
        />
</RelativeLayout>

是图像结果: enter image description here

我想要imageview“POWEREB BY FOURSQUARE”是中心 enter image description here

2 个答案:

答案 0 :(得分:0)

为headerView设置重力,它应该居中对齐。 而不是使用

headerView.setLayoutParams(new AbsListView.LayoutParams((int) CommonUtil.convertDpToPixel(151, getActivity()), (int)CommonUtil.convertDpToPixel(20,getActivity())));

使用此:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) CommonUtil.convertDpToPixel(151, getActivity()), (int)CommonUtil.convertDpToPixel(20,getActivity());
params.gravity = Gravity.CENTER_VERTICAL | Gravity.TOP
headerView.setLayoutParams(params);

答案 1 :(得分:0)

我决议成功:

RelativeLayout linearLayout = new RelativeLayout(getActivity());
    ImageView imageView = new ImageView(getActivity());
    imageView.setBackgroundResource(R.drawable.ic_powered_by_foursquare);
    RelativeLayout.LayoutParams params =
            new RelativeLayout.LayoutParams((int) CommonUtil.convertDpToPixel(151,
                                                                            getActivity()),
                                          (int) CommonUtil.convertDpToPixel(20,
                                                                            getActivity()));
    params.addRule(RelativeLayout.CENTER_IN_PARENT,
                   RelativeLayout.TRUE);
    imageView.setLayoutParams(params);
    linearLayout.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                              ViewGroup.LayoutParams.MATCH_PARENT));
    linearLayout.addView(imageView);
    mLvVenueTips.addHeaderView(linearLayout);