2016-08-31 11 views
0

私がしようとしているのは、このようなものです。デバイスがあるスクロールビュー内の線形レイアウトの重さ

enter image description here

画像1

どんなに。画面上に4行表示する必要があります。後で他の行を表示するにはスクロールしてください。

これを達成するために真の直線レイアウト重量を使用していますが、これを達成しようとしていません。私はより多くの行

enter image description here

画像を追加するときに私は、このような画面を与える2以下

画像として画面を提供しますが、より多くの行を追加すると、画像に2ビューを提供し、私のレイアウトXMLがある

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/lightGray" 
android:orientation="vertical"> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:fillViewport="true"> 

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

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:padding="1dp" 
       android:weightSum="4" 

       > 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 


        <ImageView 
         android:id="@+id/abulation" 
         style="@style/icon" 
         android:background="@drawable/wudu_96" /> 

        <ImageButton 
         android:id="@+id/masjid" 
         style="@style/icon" 
         android:background="@drawable/masjid_96" /> 


        <ImageView 
         android:id="@+id/restroom" 
         style="@style/icon" 
         android:background="@drawable/restroom_96" /> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

        <ImageView 
         android:id="@+id/dressing" 
         style="@style/icon" 
         android:background="@drawable/dressing_96" /> 

        <ImageButton 
         style="@style/icon" 
         android:background="@drawable/icon_selector" 
         android:scaleType="fitXY" /> 


        <ImageButton 
         android:id="@+id/Favourite" 
         style="@style/icon" 
         android:background="@drawable/favourite_96" 
         android:onClick="random" 
         android:scaleType="fitXY" /> 


       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

        <ImageView 
         style="@style/icon" 
         android:background="@drawable/fear_96" /> 

        <ImageView 
         style="@style/icon" 
         android:background="@drawable/fear_96" /> 

        <ImageView 
         style="@style/icon" 
         android:background="@drawable/fear_96" /> 
       </LinearLayout> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

        <ImageView 
         style="@style/icon" 
         android:background="@drawable/fear_96" /> 

        <ImageView 
         style="@style/icon" 
         android:background="@drawable/fear_96" /> 

        <ImageView 
         style="@style/icon" 
         android:background="@drawable/fear_96" /> 
       </LinearLayout> 

      </LinearLayout> 



     </LinearLayout> 
    </ScrollView> 
    </android.support.v4.widget.NestedScrollView> 
</LinearLayout> 

私はこれを達成するために間違ったことをしていることを知っています。どうか私は私が望むビューを得ることができます助けてください。

+0

あなたがグリッドにリサイクルビューを使用していない理由は?私はこれを達成するための良い方法だと思います –

+0

最初のlinearLayout(android:weightSum = "1.0")と内側のもの(android:layout_width = "0dp" android:layout_weight = "0.25" - それはあなたに4つの等しい行を与えるかどうか参照してください – Tasos

+0

これらのカテゴリは静的ですか? – Nidhi

答えて

0

これを試してください。 ScrollViewのもう1つの親LinearLayoutにビューをラップすると、問題の原因になることがあります。

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

    android:orientation="vertical"> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/toolbar" 
      android:fillViewport="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:weightSum="4"> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:weightSum="4"> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:layout_height="0dp" 
        android:layout_weight="1"> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
        <ImageView 
         android:src="@mipmap/ic_launcher" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 
       </LinearLayout> 
      </LinearLayout> 
     </ScrollView> 
    </android.support.v4.widget.NestedScrollView> 
</LinearLayout> 
0

これを試してください。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/lightGray" 
android:orientation="vertical"> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:fillViewport="true"> 

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

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:padding="1dp" 
       android:weightSum="4" 

       > 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 


        <ImageView 
         android:id="@+id/abulation" 
         android:layout_width="wrap_content" 
         android:layout_weight="1" 
         android:layout_height="match_parent" 
         android:background="#FF0F0F" /> 

        <ImageButton 
         android:id="@+id/masjid" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#D0060F" /> 


        <ImageView 
         android:id="@+id/restroom" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#1F0F0F" /> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

        <ImageView 
         android:id="@+id/dressing" 
         android:layout_width="wrap_content" 
         android:layout_weight="1" 
         android:layout_height="match_parent" 
         android:background="#FF0F0F" /> 

        <ImageButton 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#1F0F0F" 
         android:scaleType="fitXY" /> 


        <ImageButton 
         android:id="@+id/Favourite" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#D0060F" 
         android:onClick="random" 
         android:scaleType="fitXY" /> 


       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

        <ImageView 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#D0060F" /> 

        <ImageView 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#1F0F0F" /> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_weight="1" 
         android:layout_height="match_parent" 
         android:background="#FF0F0F" /> 
       </LinearLayout> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

        <ImageView 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#1F0F0F" /> 

        <ImageView 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:layout_width="wrap_content" 
         android:background="#D0060F" /> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_weight="1" 
         android:layout_height="match_parent" 
         android:background="#FF0F0F" /> 
       </LinearLayout> 

      </LinearLayout> 



     </LinearLayout> 
    </ScrollView> 
    </android.support.v4.widget.NestedScrollView> 
</LinearLayout> 

希望します。まず、私たちが主垂直LinearLayoutと水平LinearLayoutのそれぞれからandroid:layout_weightからandroid:weightSumプロパティを取り除く:

0

は、ここであなたがする必要があるものです。 idLinearLayoutandroid:[email protected]+id/mainContainerLLと設定する必要があります。 Javaのコーディング部分は、今

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:fillViewport="true"> 

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

      <LinearLayout 
       android:id="@+id/rowContainerLL" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:padding="1dp"> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_red_light" 
        android:orientation="horizontal" 
        android:weightSum="3"> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" 

        android:orientation="horizontal" 
        android:weightSum="3"> 


       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_red_light" 

        android:orientation="horizontal" 
        android:weightSum="3"> 
       </LinearLayout> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" 

        android:orientation="horizontal" 
        android:weightSum="3"> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/holo_red_light" 

        android:orientation="horizontal" 
        android:weightSum="3"> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" 

        android:orientation="horizontal" 
        android:weightSum="3"> 

       </LinearLayout> 

      </LinearLayout> 


     </LinearLayout> 
    </ScrollView> 
</android.support.v4.widget.NestedScrollView> 

::これは私があなたのレイアウトからテストのために得られたサンプルのレイアウトである私たちは、実行時に高さを取得するためにViewTreeObserverを使用します(オブザーバーができますビューが描画されようとしているときにそのコールバックが計算され、その高さが計算されると、私たちはそれを必要とします)。そこに、私たちは、水平LinearLayoutのそれぞれに高さを設定します。

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rowContainerLL); 
    ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); 

    viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     @Override 
     public boolean onPreDraw() { 
      linearLayout.getViewTreeObserver().removeOnPreDrawListener(this); 

      View child; 
      LinearLayout.LayoutParams params; 
      final int viewHeight = linearLayout.getHeight()/4; 

      for (int i = 0; i < linearLayout.getChildCount(); i++) { 
       child = linearLayout.getChildAt(i); 

       params = (LinearLayout.LayoutParams) child.getLayoutParams(); 
       params.height = viewHeight; 

       child.setLayoutParams(params); 
      } 

      return false; 
     } 
    }); 
+0

私はgetWindowManager()。getDefaultDisplay()。getMetrics();を使用しています。 4で割り、メトリック。高さは私に高さを与えます。それは良い仕事と私の問題を解決した。私のコードとあなたのコードとの違いは何ですか? – FaisalAhmed

+2

@FaisalAhmed私はそれもやってみましたが、私はステータスバー/通知バーにやや不正確な値を渡していたと思いますので、これを元に戻しました。また、ViewTreeObserverを使用して画面全体にまたがるビューコンテナを処理する必要があるときは、(ViewTreeObserver'に起因して)値が正当なものであると確信しているので、単に 'getHeight()描画可能なスペースを正確に返します。しかし、私たちがどのような方法を使用しても大きな違いはありません。 – Shaishav

+0

だから私は正しい方法でそれをやりました。そして、あなたの提案は本当に完全な感謝の助けになりました – FaisalAhmed

関連する問題