2017-08-25 169 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

相關或重複? [爲什麼我的android自定義視圖不是方形的?](https://stackoverflow.com/questions/17228741/why-is-my-android-custom-view-not-square) - 順便說一句,我只得到一個方形的縱向視圖如果我省略'android:layout_alignParentTop =「true」' – 0X0nosugar

回答

0

我不知道是什麼的問題可能是你使用的是onMeasure()技術是什麼,但檢查出SquareLayout,一個的Android庫提供一個用於不同佈局的包裝類,使得它們的平方尺寸不會丟失任何核心功能。

尺寸是在渲染布局之前計算的,因此一旦獲得視圖就沒有重新渲染或任何可以調整的尺寸。這將在每一個工作,所有場景(包括視圖方向變化

要使用庫,添加到您的build.gradle:

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

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

注:XML預覽可爲被打破這一刻,但它運行應用程序時,它工作得很好。