2017-08-25 6 views
1

正方形のビューがランドスケープモードで動作しないのはなぜですか?ランドスケープで四角形のレイアウトが機能しない

それはonMeasure方法は次の通りです:ここでは

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 
    size = Math.min(width, height); 
    setMeasuredDimension(size, size); 
} 

を、あなたは(灰色で)私の見解の背景を見ると、私の見解では、正方形ではないこと。どうして?

enter image description here

そして、ここに私の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:orientation="vertical" 
    android:padding="@dimen/md_dialog_frame_margin"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal"> 

     <com.my.package.GridPreviewView 
      android:id="@+id/grid" 
      android:background="#cccccc" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/npCols" 
      android:layout_alignParentTop="true" 
      android:layout_marginBottom="8dp" 
      android:layout_marginLeft="8dp" 
      android:layout_toRightOf="@+id/npRows" /> 

     <com.shawnlin.numberpicker.NumberPicker 
      android:id="@+id/npRows" 
      android:layout_width="48dp" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/grid" 
      android:layout_alignTop="@+id/grid" 
      android:paddingRight="8dp" 
      app:np_dividerColor="?attr/colorAccent" 
      app:np_orientation="vertical" 
      app:np_selectedTextColor="?attr/colorAccent" 
      app:np_selectedTextSize="22sp" 
      app:np_textSize="18sp" 
      app:np_width="48dp" /> 

     <com.shawnlin.numberpicker.NumberPicker 
      android:id="@+id/npCols" 
      android:layout_width="wrap_content" 
      android:layout_height="48dp" 
      android:layout_alignLeft="@+id/grid" 
      android:layout_alignParentBottom="true" 
      android:layout_alignRight="@+id/grid" 
      android:paddingTop="8dp" 
      app:np_dividerColor="?attr/colorAccent" 
      app:np_height="48dp" 
      app:np_orientation="horizontal" 
      app:np_selectedTextColor="?attr/colorAccent" 
      app:np_selectedTextSize="22sp" 
      app:np_textSize="18sp" /> 

    </RelativeLayout> 
</LinearLayout> 
+0

関連するか重複していますか? [なぜ私のアンドロイドカスタムビューは正方形ではありませんか?](https://stackoverflow.com/questions/17228741/why-is-my-android-custom-view-not-square) - btw 'android:layout_alignParentTop =" true "'を省略すると、 – 0X0nosugar

答えて

0

は、提供Androidのライブラリ問題は、あなたが使用しているonMeasure()技術であるかもしれないものを正確にわからないけど、SquareLayoutをチェックアウトさまざまなレイアウトのラッパークラスで、コアの機能を失うことなく、四角形の寸法をレンダリングします。

レイアウトがレンダリングされる直前にサイズが計算されるため、ビューが取得された後に再レンダリングや調整などが行われません。これはあなたのbuild.gradleにこれを追加し、ライブラリを使用するには(表示方向の変更を含む)すべての、すべてのシナリオで

に動作します:

repositories { 
    maven { 
     url "https://maven.google.com" 
    } 
} 

dependencies { 
    compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3' 
} 

注:XMLのプレビューがために破損する可能性が瞬間、それは完全に正常にアプリを実行すると動作します。

関連する問題