【发布时间】:2018-11-10 06:19:23
【问题描述】:
大家好。我是安卓新手。我对RecyclerView 有一些问题。我想让我的数据图像及其文本的外观垂直,但它看起来是水平的。我已经尝试在RecyclerView 小部件中将orientation 写为vertical,它是FrameLayout,在CardView。即使我把 android:scrollbars 作为垂直。但是这两个都没有帮助。
这里是RecyclerView的水平结果:
生成的图像
fragment_blank.xml 文件:
<FrameLayout 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"
tools:context="com.example.ganz.afex_with_default_navigation.fragments.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
卡片视图:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/cardview_id"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
cardview:cardCornerRadius="4dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tovar_img_id"
android:layout_width="match_parent"
android:layout_height="225px"
android:background="#2d2d2d"/>
<TextView
android:id="@+id/tovar_title_id"
android:textColor="@color/redable"
android:textSize="20sp"
android:paddingLeft="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tovar"/>
<!-- </ImageView>-->
</LinearLayout>
</android.support.v7.widget.CardView>
更新: RecyclerView 初始化:
package com.example.ganz.afex_with_default_navigation.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.ganz.afex_with_default_navigation.R;
import com.example.ganz.afex_with_default_navigation.adapters.Recyclerview_Adapter;
import com.example.ganz.afex_with_default_navigation.models.Tovar_tavsiya;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
//Tovar list BEGIN
List<Tovar_tavsiya> lstTovar;
lstTovar = new ArrayList<>();
lstTovar.add(new Tovar_tavsiya("Modadagi kiyimlar", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_1));
lstTovar.add(new Tovar_tavsiya("Chet eldan uskunalar", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_2));
lstTovar.add(new Tovar_tavsiya("Tekstil mahsulotlari", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_3));
RecyclerView myrv = (RecyclerView) v.findViewById(R.id.recyclerview_id);
Recyclerview_Adapter myrv_Adapter = new Recyclerview_Adapter(getContext(), lstTovar);
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
myrv.setAdapter(myrv_Adapter);
return v;
}
}
【问题讨论】:
-
您可能会选择
LinearLayoutManager.HORIZONTAL。在 java 文件中显示您的Recyclerview初始化。
标签: android android-recyclerview vertical-alignment android-cardview