2017-07-21 18 views
1

私はcardviewの幅を測定し、私は、質問と一緒に画像を添付します明確な理解を得るために、その外側にあるcardviewにマッチさせたいです内部カードビューが返信チャットバブルであり、アウターがチャットメッセージバブルである場合、cardviewは外側カードビューの幅と同じでなければなりません。recyclerviewのビューの幅を他のビューに依存させる方法は? 、インナーをここで</p> <p><a href="https://i.stack.imgur.com/idDVU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/idDVU.png" alt="enter image description here"></a></p> <p>:

私はcardviewを拡張しようとしている:

public class ExtendCardView extends CardView { 

Integer newWidth; 

public ExtendCardView(Context context) { 
    super(context); 
} 

public ExtendCardView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 


@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    newWidth= MeasureSpec.getSize(widthMeasureSpec); 
     super.onMeasure(newWidth, heightMeasureSpec); 

} 

public Integer getNewWidth() { 
    return newWidth; 
} 

public void setNewWidth(Integer newWidth) { 
    this.newWidth = newWidth; 
}} 

とアダプタのonBindViewに私は幅を変更するためにビューツリーオブザーバを追加しました:達成するための最適な方法です

((CardView) holder).inner_card_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @SuppressLint("NewApi") 
     @SuppressWarnings("deprecation") 
     @Override 
     public void onGlobalLayout() { 
      //((CardView) holder).inner_card_view.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY); 

      ((CardView) holder).inner_card_view.setNewWidth(((CardView) holder).outer_card_view.getNewWidth()); 
      //now we can retrieve the width and height 
      //... 
      //do whatever you want with them 
      //... 
      //this is an important step not to keep receiving callbacks: 
      //we should remove this listener 
      //I use the function to remove it based on the api level! 

      if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) 
       ((CardView) holder).inner_card_view.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      else 
       ((CardView) holder).inner_card_view.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     } 
    }); 

何このようなものは、ここに私のレイアウトは、cardviewのためのファイルです:

 <com.example.ui.view.ExtendCardView 
    android:id="@+id/outer_card" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginEnd="8dp" 
    android:layout_marginLeft="50dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginStart="50dp" 
    app:cardCornerRadius="8dp"> 


     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="2dp"> 

      <TextView 
       android:id="@+id/tv_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:textAppearance="@style/TextAppearance.AppCompat.Small" 
       android:textColor="@color/black" /> 

      <com.example.ui.view.ExtendCardView 
       android:id="@+id/inner_card" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tv_name" 
       android:layout_margin="1dp" 
       android:minWidth="80dp" 
       app:cardBackgroundColor="@color/red" 
       app:cardCornerRadius="8dp" 
       app:cardElevation="0dp"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <RelativeLayout 
         android:id="@+id/rel_layout" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="5dp" 
         android:layout_marginEnd="15dp" 
         android:layout_marginRight="15dp"> 

         <TextView 
          android:id="@+id/tv_user_name" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentLeft="true" 
          android:layout_alignParentStart="true" 
          android:textStyle="bold" /> 

         <LinearLayout 
          android:id="@+id/lin_layout" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_below="@id/tv_user_name" 
          android:layout_marginBottom="8dp" 
          android:orientation="horizontal"> 

          <ImageView 
           android:id="@+id/iv_type" 
           android:layout_width="18dp" 
           android:layout_height="18dp" 
           android:layout_marginEnd="4dp" 
           android:layout_marginRight="4dp" 
           android:tint="@color/dark_grey" 
           android:visibility="gone" /> 

          <TextView 
           android:id="@+id/tv_message" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:autoLink="web" 
           android:maxLines="3" /> 
         </LinearLayout> 


        </RelativeLayout> 

        <ImageView 
         android:id="@+id/iv_media" 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:layout_gravity="center_vertical" 
         android:layout_marginEnd="8dp" 
         android:layout_marginLeft="15dp" 
         android:layout_marginRight="8dp" 
         android:layout_marginStart="15dp" 
         android:visibility="gone" /> 
       </LinearLayout> 

      </com.nodd.nodd.ui.view.ExtendCardView> 

      <TextView 
       android:id="@+id/tv_reply_message" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/inner_card" 
       android:layout_marginBottom="5dp" 
       android:layout_marginEnd="4dp" 
       android:layout_marginLeft="4dp" 
       android:layout_marginRight="4dp" 
       android:layout_marginStart="4dp" 
       android:autoLink="web" 
       android:linksClickable="true" 
       android:textColor="@color/white" 
       android:textColorLink="@color/linkedin_blue" /> 

      <ImageView 
       android:id="@+id/tv_reply_image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/inner_card" 
       android:visibility="gone" 
       android:layout_marginBottom="5dp" 
       android:layout_marginEnd="4dp" 
       android:layout_marginLeft="4dp" 
       android:layout_marginRight="4dp" 
       android:layout_marginStart="4dp" 
       android:layout_marginTop="5dp" /> 
     </RelativeLayout> 

    </RelativeLayout> 
</com.example.ui.view.ExtendCardView> 
+0

内部cardviewのmatch_parent? –

+0

カードを画面のサイズに合わせて伸ばします – headshoT

+0

返信用ポストサイズに合わせて調整されたネストされたCardViewが必要です。返信ポストもCardViewにあります。両方のカードで 'android:setWidth =" wrap_content "'を試してみてください。 CardViewの内部では、 'android:layout_margin =" 5dp "'を追加するだけです。 –

答えて

0

カードビューdefauあなたのカードルート(RelativeLayout、LinearLayout、...)の他のレイアウトを使用してCardViewから拡張しカスタマイズするCustomCardViewクラスを作成する

0

これは完璧かどうかわかりませんがあなたの問題を解決するはずです。内側のカードが外側のカードと同じ幅にならないようにする場所は2つあります。内側のカードに設定した1dpのマージン&内側のカードの親であるRelativeLayoutに設定した2dpのパディング。これらの値を両方とも0に設定すると、内側のカードを外側と同じ幅にすることができます。 RelativeLayoutにIDを付ける必要があります。私はcard_layoutを使用しました。次に、マージンがあるかどうかを知るところに、このロジックを配置します。

int margin; 
    int padding; 

    if(needsMargins) { 
     margin = 1; 
     padding = 2; 
    } else { 
     margin = 0; 
     padding = 0; 
    } 

    CardView cardView = (CardView)findViewById(R.id.inner_card); 
    ViewGroup.MarginLayoutParams params = 
      (ViewGroup.MarginLayoutParams)cardView.getLayoutParams(); 
    params.setMargins(margin, margin, margin, margin); 
    cardView.requestLayout(); 

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.card_layout); 
    relativeLayout.setPadding(padding, padding, padding, padding); 
1

ConstraintLayoutをご覧ください。

ConstraintLayoutあなたは、フラットビュー階層(なしネストされたビュー・グループ)と大規模で複雑なレイアウトを作成することができます。 RelativeLayoutと似ていますが、すべてのビューは兄弟ビューと親レイアウトの関係に従ってレイアウトされていますが、RelativeLayoutよりも柔軟で、Android Studioのレイアウトエディタで使いやすくなっています。

あなたはConstraintLayoutにあなたの内側のカードを入れて、大きさを測定し、設定せずに、外カードによって、その大きさを調整することができます。 match_parentので

は、ConstraintLayoutに許容0dp(内側と外側のカードの間にスペースを追加するlayout_margin特性を調整)にlayout_height及び/又はlayout_widthに設定されていません。

<com.example.ui.view.ExtendCardView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 

    android:id="@+id/inner_card" 
    android:layout_margin="1dp" 
    android:minWidth="80dp" 
    app:cardBackgroundColor="@color/red" 
    app:cardCornerRadius="8dp" 
    app:cardElevation="0dp"> 

    <!-- ExtendCardView content --> 

</com.nodd.nodd.ui.view.ExtendCardView> 

はそれが役に立てば幸い:あなたのケースでは

次のような何かを得るだろう!

関連する問題

 関連する問題