2017-08-09 6 views
0

私はAndroidの開発の初心者:) 私はグリッドrecyclerViewを使用していますが、私は唯一の2 に列の数を設定しているんだと言う必要があります開始する前に私のcardViewImageViewが1つあります。このカード(またはイメージ)を画面幅の50%にする方法がわかりません。なぜなら、数字で高さと幅を設定すると、異なる画面サイズで問題が発生するからです。ImageViewの幅は、Androidデバイスの幅の50%であることを

layout_weightでいくつかの方法を試しましたが、これはcardViewとは動作しません。私はここでそれを行うことができますどのように

card.xml

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

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_photo_double_view" 
     android:src="@drawable/e48" 
     /> 


</LinearLayout> 

おかげで、事前に

+0

'CardView'がFrameLayout''から延びています。あなたが本当に 'CardView'を使いたいならば、' LinearLayout'をCardViewの中に入れて、次にattr layout_weightを使うことができます –

答えて

1

このような事がConstraintLayoutにapperedています。あなたはそれについてherehereを読むことができます。あなたが見ることができるように

<?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:orientation="horizontal"> 

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

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" /> 

    <FrameLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 
</LinearLayout> 

:私は唯一の提案あなたはこのようLinearLayoutを使用することができ、このライブラリを使用せずに

。私はandroid:weight属性をLinearLayoutとし、linearlayoutが画面全体をとった場合、ImageViewが画面幅の50%を占めるようにします。

weight_sumをLinearLayoutで定義する必要はありません。単に子レイアウトでlayout_weightを使用して値を割り当てるだけです。パーティションを垂直または水平にしたい場合は、heightまたはwidthを必ず0に設定してください。

PS。 layout_weightはLinearLayoutsでのみ機能します。

0

相対レイアウトの割合を使用することで、これを簡単に達成できます。ここでは、percentRelativeLayoutの実装方法について説明します。

依存関係:あなたのレイアウトファイルで

compile 'com.android.support:percent:23.3.0' 

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 


    <ImageView 
     app:layout_widthPercent="match_parent" 
     app:layout_heightPercent="50%"/> 

</android.support.percent.PercentRelativeLayout> 

希望これは便利です:)

関連する問題