在RelativeLayout中居中水平RecyclerView

时间:2017-10-04 17:09:44

标签: android android-layout android-recyclerview

我正在尝试将我的水平RecyclerView设置在RelativeLayout的中心。但是有些列表项目按照我的要求获得了中心,而其中一些项目并没有设置为中心。看起来像这样。 enter image description here

我的XML看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:background="?attr/colorPrimaryDark"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:textColor="?attr/TextColor"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/toolbar"
        android:text="=: Change Your Font :="
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textSize="@dimen/appName"
        android:id="@+id/title"
        android:padding="@dimen/lblTime" />

    <LinearLayout
        android:background="?attr/TextColor"
        android:orientation="vertical"
        android:layout_below="@+id/title"
        android:layout_above="@+id/dialog_dismiss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="2dp"
            android:layout_weight="7">


            <TextView
                android:scrollbars="vertical"
                android:id="@+id/font_text_quote"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:linksClickable="true"
                android:gravity="center"
                android:layout_centerVertical="true"
                android:padding="10dp"
                android:text="@string/sample"
                android:textColor="?attr/colorPrimaryDark"
                android:textSize="@dimen/newMainText"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_gravity="center"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1">

        <android.support.v7.widget.RecyclerView
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:id="@+id/hsv_font_bartextview"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar"
            android:layout_gravity="center"
            android:visibility="visible" />
        </RelativeLayout>

    </LinearLayout>




    <TextView
        android:id="@+id/dialog_dismiss"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/toolbar"
        android:text="Dismiss"
        android:textStyle="bold"
        android:textColor="?attr/TextColor"
        android:padding="@dimen/lblTime"
        android:textSize="@dimen/appName"
        android:gravity="center"
        android:layout_marginEnd="@dimen/lblTime"
        android:layout_marginRight="@dimen/lblTime"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

My Child Item XML就像这样

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    style="@style/SelectableItemBackground"
    android:gravity="center"
    android:orientation="vertical"
    >
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardView"
    android:layout_gravity="center_vertical"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp">


    <TextView
        android:id="@+id/textView"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:background="?attr/TextColor"
        android:gravity="center"
        android:text="hello"
        android:textColor="?attr/colorPrimaryDark"
        android:textSize="21sp"
        android:visibility="visible" />

</android.support.v7.widget.CardView>
    </LinearLayout>

我的Java代码如下所示

private void showGotoPageDialog() {

        Typeface font = Typeface.createFromAsset(SettingsActivity.this.getAssets(), constant.font);
        final Dialog mDialog = new Dialog(SettingsActivity.this);
        mDialog.setContentView(R.layout.font_dialogue);
        mDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayoutManager layoutManager
                = new LinearLayoutManager(SettingsActivity.this, LinearLayoutManager.HORIZONTAL, false);
        hsv_font_bartextview= mDialog.findViewById(R.id.hsv_font_bartextview);
        hsv_font_bartextview.setLayoutManager(layoutManager);

        final TextView font_text_quote;
        TextAdapterTextview textAdaptertextview = new TextAdapterTextview(SettingsActivity.this, Globle.getFontArray());
        hsv_font_bartextview.setAdapter(textAdaptertextview);

        font_text_quote= mDialog.findViewById(R.id.font_text_quote);
        font_text_quote.setTypeface(font);
        textAdaptertextview.setOnClickLIstner(new OnTClickLIstner() {
            @Override
            public void onClick(View v, String image, int position) {
                constant.font=image;
                Typeface newfont = Typeface.createFromAsset(SettingsActivity.this.getAssets(), constant.font);
                font_text_quote.setTypeface(newfont);

            }
        });

        mDialog.show();
        TextView dismiss = (TextView) mDialog.findViewById(R.id.dialog_dismiss);
        dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                Intent intent = new Intent(SettingsActivity.this, HomeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }

}

适配器Java代码

public class TextAdapterTextview extends RecyclerView.Adapter<TextAdapterTextview.MyViewHolder> {

    public OnTClickLIstner onClickLIstner;
    Activity activity;
    private ArrayList<String> dataSet;

    public TextAdapterTextview(Activity activity, ArrayList<String> data) {
        this.dataSet = data;
        this.activity = activity;
    }

    public void setOnClickLIstner(OnTClickLIstner onClickLIstner) {
        this.onClickLIstner = onClickLIstner;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.text_view_raw_text, parent, false);
        //view.setOnClickListener(MainActivity.myOnClickListener);

        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
        holder.textView.setText(Globle.getfontnames().get(listPosition));
        holder.textView.setTypeface(Typeface.createFromAsset(activity.getAssets(), dataSet.get(listPosition)));
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
            itemView.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            if (onClickLIstner != null) {
                onClickLIstner.onClick(v, dataSet.get(getAdapterPosition()), getAdapterPosition());
            }
        }
    }
}

你可以看到前两个项目放在中心,最后两个项目也位于中心,但第三个项目位于顶部而不是中心。如果有人有想法纠正它,请告诉我。

修改 当我滚动项目并点击项目时,它在中心的设置是正确的。

由于

2 个答案:

答案 0 :(得分:1)

更改android:layout_height =&#34; @ dimen / toolbar&#34;

到android:layout_height =&#34; match_parent&#34;

<android.support.v7.widget.RecyclerView
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:id="@+id/hsv_font_bartextview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:visibility="visible" />

这应该解决问题。

答案 1 :(得分:0)

对于RelativeLayout,gravity属性由以下项设置:

android:layout_centerInParent="true"