2017-05-18 27 views
1

我有一個片段在頂部包含另一個片段,然後是一個水平LinearLayout,它有兩列,總weightSum = 3。左邊的列是一個帶有TextView和ListView的垂直RelativeLayout(權重爲1)。右邊是一個帶有TextView和GridView和Button的垂直LinearLayout(權重2)。牛軋糖的間距發生了巨大變化。無論是在Studio模擬器還是我自己的三星S7 Edge。此外,仿真器報告不同的屏幕布局。對於Nexus 6P(1440 x 2560),API 23報告LARGE,其中API 25報告NORMAL。我的三星s7 Edge(1440 x 2560)也報告正常,並運行7.0(API 24)。在Nougat 7.0 API 24和7.1 API中更改了Android佈局間距

在API 24及更高版本中,正確的佈局緊靠右邊緣擠壓,好像重量被忽略。

API 23正常工作。我使用了相同的仿真器類型,6P和不同的API。

這裏是API 23的樣子: enter image description here

而且這裏是API 25的樣子:

enter image description here

這裏是我的片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/fragment_course" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
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="com.hubbardsoftware.racetac.CourseActivityFragment" 
tools:showIn="@layout/activity_course"> 

<fragment 
    android:name="com.hubbardsoftware.racetac.TimerFragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:layout="@layout/fragment_timer" /> 

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

    <RelativeLayout 
     android:id="@+id/course_part" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/course_course_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/title_course" 
      android:gravity="center" 
      android:textSize="@dimen/text_course_title" 
      android:textColor="#ff000000" 
      android:textStyle="bold" /> 

     <ListView 
      android:id="@+id/listview_course" 
      android:layout_below="@id/course_course_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:layout_toEndOf="@id/course_part" 
     android:layout_toRightOf="@id/course_part" 
     android:layout_gravity="right" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/course_marks_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center_horizontal" 
      android:text="@string/title_marks" 
      android:textSize="@dimen/text_course_title" 
      android:textColor="#ff000000" 
      android:textStyle="bold" /> 

     <GridView 
      android:id="@+id/gridview_course" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:numColumns="auto_fit" 
      android:stretchMode="columnWidth" /> 

     <Button 
      android:id="@+id/button_clear_course" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_rounded" 
      android:backgroundTint="@color/clearCourseButton" 
      android:text="@string/clear_all" 
      android:textSize="@dimen/text_course_clear" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/course_length" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center_horizontal" 
      android:text="Length" 
      android:textSize="@dimen/text_course_length" 
      android:textColor="#ff000000" 
      android:textStyle="bold" /> 

    </LinearLayout> 

</LinearLayout> 

任何想法爲什麼變化?看起來這可能與以下的API 24和25是真的爲這個尺寸的設備:

((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) 

而這是等於Configuration.SCREENLAYOUT_SIZE_LARGE在API 23

+0

附加信息:如果我將RelativeLayout寬度更改爲0dp(從200dp開始),它在API 24和25上可以正常工作。但是,在API 23上它現在壓縮右列,儘管沒有上面那麼嚴重。我可能需要添加Java代碼來檢測轉速並在顯示前手動更改該值。 –

+0

我找到了解決方法。看到我的「答案」,儘管我仍然想知道是否有人知道在XML中執行此操作的「乾淨」方式。 –

回答

0

這段Java代碼的解決方法解決這個問題,儘管如果有人知道的話,我仍然會喜歡一個合適的XML解決方案。我原樣離開了XML,RelativeLayout的寬度爲200dp。然後我在片段onCreateView方法中執行以下操作:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
    { 
     RelativeLayout coursePart = (RelativeLayout) rootView.findViewById(R.id.course_part); 
     ViewGroup.LayoutParams params = coursePart.getLayoutParams(); 
     params.width = 0; 
    } 

舊的和新的API都顯示與此代碼相同的內容。