2017-01-20 33 views
0

我想爲我的應用創建佈局。我已經嘗試創建,但我無法獲得所需的用戶界面。其實我更新鮮的android開發,我需要幫助提高我的android開發知識。 請在下面找到附件快照: enter image description here如何在Android中使用網格佈局創建回收視圖使用網格佈局

在此先感謝。

我想創建這種類型的佈局。 我已經在這裏創建它,這是我的代碼。 raw_layout.xml

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

 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:id="@+id/cvList" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="wrap_content" 
 
    android:clickable="true" 
 
    android:focusable="true" 
 
    android:foreground="?android:attr/selectableItemBackground" 
 
    app:cardElevation="8dp" 
 
    app:cardUseCompatPadding="true"> 
 

 
    <ScrollView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content"> 
 

 
     <HorizontalScrollView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="match_parent"> 
 

 
      <!-- Your content here --> 
 

 

 
      <ImageView 
 
       android:id="@+id/ListImage" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_marginBottom="230dp" 
 
       android:scaleType="center" 
 
       android:src="@mipmap/ic_launcher" /> 
 

 
      <RelativeLayout 
 
       android:id="@+id/footerView" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_gravity="bottom" 
 
       android:paddingBottom="@dimen/value_5dp" 
 
       android:paddingLeft="@dimen/value_15dp" 
 
       android:paddingRight="@dimen/value_15dp"> 
 

 

 
       <LinearLayout 
 
        android:layout_width="match_parent" 
 
        android:layout_height="wrap_content" 
 
        android:layout_toLeftOf="@+id/overflow" 
 
        android:orientation="vertical"> 
 

 
        <TextView 
 
         android:id="@+id/ListTitle" 
 
         android:layout_width="wrap_content" 
 
         android:layout_height="wrap_content" 
 
         android:layout_marginBottom="10dp" 
 
         android:layout_marginLeft="10dp" 
 
         android:layout_marginTop="10dp" 
 
         android:text="ListTitle" 
 
         android:textSize="@dimen/value_15sp" 
 
         android:textStyle="bold" /> 
 

 
       </LinearLayout> 
 

 

 
      </RelativeLayout> 
 

 
     </HorizontalScrollView> 
 

 
    </ScrollView> 
 

 

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

 
my output is like that [my output][2]

回答

1

請檢查下面的XML文件,您的回收站查看原始文件

raw_grid

<android.support.v7.widget.CardView  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:card_view="http://schemas.android.com/tools" 
android:id="@+id/cardView" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginBottom="@dimen/scale_10dp" 
android:layout_marginLeft="@dimen/scale_5dp" 
android:layout_marginRight="@dimen/scale_5dp" 
app:cardElevation="2dp" 
card_view:cardCornerRadius="8dp" 
card_view:cardUseCompatPadding="true"> 

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

    <android.support.v7.widget.AppCompatImageView 
     android:id="@+id/ivImages" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/scale_150dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/dummy_1" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:orientation="horizontal" 
     android:padding="@dimen/scale_10dp"> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/tvTitle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="HMS Scrap" 
      android:textColor="@color/aluminum" /> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/tvCount" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="(20)" 
      android:textColor="@color/aluminum" /> 
    </LinearLayout> 
</LinearLayout> 
</android.support.v7.widget.CardView> 

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white" 
android:orientation="vertical"> 

<include layout="@layout/toolbar" /> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/transparent" 
    android:clipToPadding="false" 
    android:paddingTop="@dimen/scale_10dp" /> 
</LinearLayout> 

MainActivity

public class SubCategoryActivity extends AppCompactActivity { 
private RecyclerView recyclerView; 
private GridLayoutManager layoutManager; 
private LinearLayout llProgress, llError; 
private AppCompatTextView tvEmpty; 
private SubCategoryAdapter mAdapter; 
private String title = ""; 

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

    init(); 
} 

private void init() { 
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
    llProgress = (LinearLayout) findViewById(R.id.llProgress); 
    llError = (LinearLayout) findViewById(R.id.llErrorMessage); 
    tvEmpty = (AppCompatTextView) findViewById(R.id.tvEmpty); 

    layoutManager = new GridLayoutManager(MainActivity.this, 2); 
    recyclerView.setLayoutManager(layoutManager); 

    mAdapter = new SubCategoryAdapter(MainActivity.this, clickListener); 
    recyclerView.setAdapter(mAdapter); 
} 
} 
+0

謝謝您的指導。 :) @Akash –