在Recyclerview中居中CardViews

时间:2018-04-25 21:59:40

标签: android

我的代码是:

MainActivity:

constructor(private router: Router, private route: ActivatedRoute) {}

this.router.navigate(['edit',id], { relativeTo: this.route });

他的布局:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

FragmentListAlarms:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    android:layout_gravity="center"
        tools:context="com.example.programista.dobra_robimy.MainActivity"
        android:orientation="vertical">

        <fragment
            android:id="@+id/list_frag"
            class="com.example.programista.dobra_robimy.FragmentListAlarms"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
        />
</LinearLayout>

他的布局:

public class FragmentListAlarms extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        RecyclerView alarmsRecycler = (RecyclerView) inflater.inflate(R.layout.fragment_list_alarms, container, false);

        String[] alarmsNames = new String[Alarms.AlarmsList.length];
        for (int i = 0; i < alarmsNames.length; i++) {
            alarmsNames[i] = Alarms.AlarmsList[i].getName();
        }

        TextAdapter adapter = new TextAdapter(alarmsNames);
        alarmsRecycler.setAdapter(adapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        alarmsRecycler.setLayoutManager(layoutManager);
        return alarmsRecycler;
    }
}

适配器:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/alarms_recycler"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:scrollbars="vertical" />

和cardview布局:

class TextAdapter extends RecyclerView.Adapter<TextAdapter.ViewHolder> {
    private String[] array;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private CardView cardView;

        public ViewHolder(CardView v) {
            super(v);
            cardView = v;
        }
    }

    public TextAdapter(String[] names) {
        this.array = names;
    }

    @Override
    public TextAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int    viewType) {
        CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
        return new ViewHolder(cv);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CardView cardView = holder.cardView;
        TextView textView = (TextView) cardView.findViewById(R.id.info_text);
        textView.setText(array[position]);
    }

    @Override
    public int getItemCount() {
        return array.length;
    }
}

我的问题:我想在屏幕上的中心位置<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="5dp" card_view:cardCornerRadius="20dp"> <TextView android:id="@+id/info_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" /> </android.support.v7.widget.CardView> ,但我总是得到类似的东西: screen shot from phone

我设置了引力=&#34;中心&#34;在每个可能的地方,但CardViews仍然坚持左边距:(

1 个答案:

答案 0 :(得分:0)

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"//the more this value the less width of card view
    android:layout_marginEnd="32dp"//the more this value the less width of card view
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    card_view:cardCornerRadius="20dp">

    <TextView
        android:id="@+id/info_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp" />

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

修改 据我所知,通过为父视图设置 layout_gravity =“center”,无法简单地集中使用RecyclerView的项目,但还有另一种方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">   

<android.support.v7.widget.CardView 
    android:id="@+id/card_view"
    android:layout_width="200dp"
    android:layout_gravity="center"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <TextView/>

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