2017-04-20 121 views
0

我是android新手。我正在使用Android Studio 2.3.1。我正在嘗試創建一個用戶界面,正如您可以從附加圖片中看到的那樣,LinearLayout即將出現在手機屏幕上。我嘗試過使用layout_gravity,gravity等幾個屬性,但是沒有一個能夠在手機屏幕中看到LinearLayout。Android UI LinearLayout出「手機屏幕」

這是我目前的佈局行爲的截圖:

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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 


    <LinearLayout 
     android:id="@+id/topSection" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#DDDDDD" 
     android:orientation="horizontal" 
     android:padding="5dp" 
     android:weightSum="1"> 

     <LinearLayout 
      android:layout_width="264dp" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:id="@+id/imageButton" 
       android:layout_width="82dp" 
       android:layout_height="51dp" 
       android:layout_weight="1" 

       android:scaleType="centerCrop" 
       android:src="@mipmap/leaf" 
       app:srcCompat="@mipmap/leaf" /> 

      <TextView 
       android:id="@+id/textView" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="OFIS Companion" /> 
     </LinearLayout> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00000000" 
      android:backgroundTint="#00000000" 
      app:srcCompat="@mipmap/settingscog" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middleSection" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:editable="false" 
      android:text="Start" 
      android:textSize="24sp" /> 

     <LinearLayout 
      android:layout_width="180dp" 
      android:layout_height="371dp" 
      android:layout_gravity="bottom" 
      android:baselineAligned="false" 
      android:clickable="false" 
      android:duplicateParentState="false" 
      android:fadeScrollbars="false" 
      android:filterTouchesWhenObscured="false" 
      android:fitsSystemWindows="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:forceHasOverlappingRendering="false" 
      android:hapticFeedbackEnabled="false" 
      android:isScrollContainer="false" 
      android:keepScreenOn="false" 
      android:layerType="none" 
      android:longClickable="false" 
      android:orientation="horizontal"> 
     </LinearLayout> 

    </LinearLayout>  
</LinearLayout> 
+0

其原因在於其方向是水平的..將其改爲垂直.. – ZeroOne

+0

'middleSection'' LinearLayout'是水平的,我相信@ZeroOne指的是。其中的'TextView'具有'match_parent'作爲'layout_width',所以它將另一個'LinearLayout'推到了外面。 –

+0

感謝Mike和ZeroOne。這使它工作! –

回答

0

的問題是你middleSection的LinearLayout方向是horizontal,並且您已經使用TextView寬度match_parentTextView消耗完全widthmiddleSection LinearLayout這就是爲什麼child LineraLayout顯示在屏幕之外。

change middleSection LinearLayout orientation to vertical

試試這個:

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 


    <LinearLayout 
     android:id="@+id/topSection" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#DDDDDD" 
     android:orientation="horizontal" 
     android:padding="5dp" 
     android:weightSum="1"> 

     <LinearLayout 
      android:layout_width="264dp" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:id="@+id/imageButton" 
       android:layout_width="82dp" 
       android:layout_height="51dp" 
       android:layout_weight="1" 

       android:scaleType="centerCrop" 
       android:src="@mipmap/leaf" 
       app:srcCompat="@mipmap/leaf" /> 

      <TextView 
       android:id="@+id/textView" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="OFIS Companion" /> 
     </LinearLayout> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00000000" 
      android:backgroundTint="#00000000" 
      app:srcCompat="@mipmap/settingscog" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middleSection" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:editable="false" 
      android:text="Start" 
      android:textSize="24sp" /> 

     <LinearLayout 
      android:layout_width="180dp" 
      android:layout_height="371dp" 
      android:layout_gravity="bottom" 
      android:baselineAligned="false" 
      android:clickable="false" 
      android:duplicateParentState="false" 
      android:fadeScrollbars="false" 
      android:filterTouchesWhenObscured="false" 
      android:fitsSystemWindows="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:forceHasOverlappingRendering="false" 
      android:hapticFeedbackEnabled="false" 
      android:isScrollContainer="false" 
      android:keepScreenOn="false" 
      android:layerType="none" 
      android:longClickable="false" 
      android:orientation="horizontal" 
      android:background="#ff0000"> 

     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

OUTPUT:

enter image description here

僅供參考,只是爲了測試我使用android:background="#ff0000"來紀念這個LinearLayout

0

由於您不知道權重和佈局權重,所以圖像視圖將不在屏幕上,因此請儘量不要在開始時使用它。

雖然你的問題我給你一個解決方案

weightsum是用來定義爲水平方向的線性佈局可用, 同樣適用於垂直線性佈局 最大高度和最大寬度現在的東西孩子的佈局,你必須按照你的意願劃分權重。

如果您在此處使用水平佈局...使用0dp或dip作爲兒童的寬度參數,並且 請確保您正在分配兒童,例如總和小於權重的layout_weights。

而不是...你必須照顧你的android基礎知識首先。

開始閱讀文檔。