2016-07-29 160 views
5

我想创建制作宽屏幕的70%的宽度

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


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

我使用的是回收的看法,但我的问题这样的效果,是每个卡的宽度的100%屏幕为70%。

下面是每个项目

<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代码的视图中的XML文件保存在回收站视图真的这样做的

<?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-partial-view-of-the-the-page) – SaravInfern

+0

你可以动态地将项目添加到viewpager吗? –

+0

是的,你可以添加/删除视图动态查看寻呼机 – SaravInfern

回答

5

两种方式。

1)为您的回收视图使用自定义视图。 Override onMeasure将其宽度返回为屏幕的70%。

2)在您的Recycler View适配器中,创建视图时,将其宽度设置为屏幕宽度的70%。

无论哪种情况,您都可以从人机界面获得屏幕大小,并将宽度乘以.7。在第一种情况下,您将其设置为EXACT测量宽度,在第二种情况下将其设置为布局参数。第二个可能更容易一些。

+0

我尝试了2,它从来没有工作,虽然它调整了视图的大小,该项目的底部和左侧的所有项目消失在屏幕之外。 如果我试过第一个答案,我会不会面临同样的问题? –

-2

尝试更改LinearLayoutPercentRelativeLayout以“包装”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" 
      /> 

编辑

自%的支持库与Android支持库23走来所以请确保您更新的Android SDK Manager中的支持库到最新版本(实际上是24)。然后添加像下面的依赖在build.gradle文件:

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

不幸的是,从来没有工作,它只是使回收者视图70%的宽度,而不是其中的项目 你看看我添加的图片,以显示我在找什么? –