2016-07-29 15 views
5

私はこの効果メイクの幅が画面幅の70%

[![Design image][1]][1] 


    [1]: http://i.stack.imgur.com/SCMA5.jpg 

を作成しようとしていますが、各カードには、幅の100%であるということです画面は70%になる。ここで

は、ここで、各項目のXMLコード

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_margin="@dimen/scale_20dp" 
    android:id="@+id/rowLayout" 
    > 



     <LinearLayout 
      android:id="@+id/button_parent" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/card_view" 
       > 



      <LinearLayout 
       android:id="@+id/currentYear" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/paymentscreengrey" 
       android:orientation="vertical" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:paddingTop="@dimen/scale_50dp" 
       android:paddingLeft="35dp"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="**** **** **** 5432" 
        android:textSize="@dimen/scale_20dp" 
        android:textColor="@color/white" 
        android:layout_marginTop="@dimen/scale_20dp" 
        /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="2345" 
        android:textSize="@dimen/scale_16dp" 
        android:textColor="@color/white" /> 



       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="match_parent" 
        android:orientation="horizontal" 
        android:paddingRight="@dimen/scale_20dp" 
        android:paddingTop="@dimen/scale_15dp" 
        > 




        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="PERSONAL" 
         android:fontFamily="bold" 
         android:textColor="@color/white" 
         android:textSize="18dp" 
         android:textStyle="bold" /> 



        <View 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_weight="1" 
         /> 



        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:text="@string/title_valid_up_to" 
         android:textColor="@color/white" 
         android:textSize="10dp" /> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="12/15" 
         android:layout_marginLeft="10dp" 
         android:textColor="@color/white" 
         android:textSize="@dimen/text_18" 
         /> 



       </LinearLayout> 


      </LinearLayout> 




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

     </LinearLayout> 



</LinearLayout> 

あるリサイクルビューに実際にこれを行うの

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

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rv" 
     /> 

</LinearLayout> 



and finally the recycler view 

    package com.heyjude.heyjudeapp.adapter; 

    import android.app.Activity; 
    import android.graphics.Point; 
    import android.support.v7.widget.CardView; 
    import android.support.v7.widget.RecyclerView; 
    import android.view.Display; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.LinearLayout; 

    import com.heyjude.heyjudeapp.R; 
    import com.heyjude.heyjudeapp.model.Card; 

    import java.util.ArrayList; 
    import java.util.List; 


    public class CheckOutCardRecycler extends RecyclerView.Adapter<CheckOutCardRecycler.CardViewHolder>{ 

     private List<Card> checkOutCard; 
     private int screenWidth; 


     public CheckOutCardRecycler(Activity activity, List<Card> checkOutCard){ 

      Display display = activity.getWindowManager().getDefaultDisplay(); 
      Point size = new Point(); 
      display.getSize(size); 
      this.screenWidth = size.x; 
      this.checkOutCard = checkOutCard; 
     } 

     public static class CardViewHolder extends RecyclerView.ViewHolder { 
      CardView card; 
      LinearLayout row; 



      CardViewHolder(View itemView) { 
       super(itemView); 
       row = (LinearLayout)itemView.findViewById(R.id.rowLayout); 
       card = (CardView)itemView.findViewById(R.id.card_view); 


      } 
     } 

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

     @Override 
     public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
      View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_checkout_cards, viewGroup, false); 
      CardViewHolder pvh = new CardViewHolder(v); 
      return pvh; 
     } 

     @Override 
     public void onBindViewHolder(CardViewHolder cardViewHolder, int i) { 




      } 

      @Override 
      public void onAttachedToRecyclerView(Recycler View recyclerView) { 
       super.onAttachedToRecyclerView(recyclerView); 
      } 

    } 
+0

の代わりに、リサイクルビューあなたは、デフォルトで必要と、この機能を持つビューページャを使用することができます[これを参照してください](http://stackoverflow.com/questions/9907748/viewpager-get-a-next-page-of-the-next-page) – SaravInfern

+0

項目を動的にビューページに追加できますか? –

+0

はい、ビューのページャを動的に追加/削除することができます – SaravInfern

答えて

5

二つの方法を保持しているビューのためのXMLファイルです。

1)リサイクルされたビューのカスタム表示を使用します。 onMeasureをオーバーライドして、画面の幅を画面の70%に戻します。

2)Recycler Viewアダプタでは、ビューを作成するときにその幅を画面の幅の70%に設定します。

いずれの場合でも、ディスプレイから画面サイズを取得し、幅を0.7倍に増やすだけです。最初のケースでは、それをEXACT測定幅として設定し、次にレイアウトパラメータで設定します。おそらくもう少し簡単です。

+0

2を試しましたが、画面のサイズをリサイズしましたが、アイテムの下部と左側のすべてのアイテムが画面からはみ出しました。 私は最初の答えを試しても同じ問題に直面しませんか? –

-2

LinearLayoutを変更してRecyclerViewを「ラップ」し、その幅を70%に設定してみてください。

変更この:これにより

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rv" 
     /> 

</LinearLayout> 

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      app:layout_widthPercent="70%" 
      android:id="@+id/rv" 
      /> 

EDIT

パーセントサポートライブラリは、Androidのサポートライブラリ23と一緒に来るのでので、あなたは、Androidを更新していることを確認してください既にSDKマネージャのライブラリをサポートしています(実際には24)。そしてbuild.gradleファイルに以下のような依存関係を追加します。

compile 'com.android.support:percent:24.0.0' 
+0

これはうまくいきませんでしたが、リサイクラーはその中のアイテムの代わりに幅の70%を表示しました 私が探しているものを見せてくれましたか? –

関連する問題