2017-08-11 59 views
-1

出於某種原因,我的框架佈局不會一直延伸到屏幕邊緣。爲什麼FrameLayout不能用作我的片段容器使用全屏寬度?

Screenshot

這裏是我的活動佈局:

<FrameLayout 
    android:id="@+id/contactInfoFragmentContainer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteX="8dp" 
    tools:layout_editor_absoluteY="8dp"> 

</FrameLayout> 

而且這裏是,我在的FrameLayout在運行時將孤片段佈局:

<!-- TODO: Update blank fragment layout --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:weightSum="1" 
    tools:layout_editor_absoluteX="8dp" 
    tools:layout_editor_absoluteY="8dp"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="220dp" 
     app:srcCompat="@android:drawable/btn_dialog" 
     tools:layout_editor_absoluteX="8dp" 
     tools:layout_editor_absoluteY="0dp" /> 

    <LinearLayout 
     android:id="@+id/thumbnailsLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.05" 
     android:orientation="horizontal"> 

     <HorizontalScrollView 
      android:id="@+id/Thumbnails" 
      android:layout_width="260dp" 
      android:layout_height="97dp"> 

     </HorizontalScrollView> 

     <Button 
      android:id="@+id/addImage" 
      android:layout_width="wrap_content" 
      android:layout_height="96dp" 
      android:layout_weight="1.06" 
      android:text="Add image" /> 
    </LinearLayout> 

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

     <TextView 
      android:id="@+id/nameTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Name" /> 

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textPersonName" /> 

    </LinearLayout> 

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

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Phone" /> 

     <Spinner 
      android:id="@+id/spinner" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <EditText 
      android:id="@+id/phoneText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="number" /> 

    </LinearLayout> 

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

     <EditText 
      android:id="@+id/addressText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textPostalAddress" /> 
    </LinearLayout> 

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

我只是做這個添加的片段在我的onCreate方法:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_shop); 

     // Check that the activity is using the layout version with 
     // the fragment_container FrameLayout 
     if (findViewById(R.id.contactInfoFragmentContainer) != null) { 

      // However, if we're being restored from a previous state, 
      // then we don't need to do anything and should return or else 
      // we could end up with overlapping fragments. 
      if (savedInstanceState != null) { 
       return; 
      } 

      // Create a new Fragment to be placed in the activity layout 
      ContactInfoFragment firstFragment = new ContactInfoFragment(); 

      // In case this activity was started with special instructions from an 
      // Intent, pass the Intent's extras to the fragment as arguments 
      firstFragment.setArguments(getIntent().getExtras()); 

      // Add the fragment to the 'fragment_container' FrameLayout 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.contactInfoFragmentContainer, firstFragment).commit(); 
     } 
    } 

那麼是什麼問題?當我在設計視圖中查看碎片佈局時,它看起來很好!

Fragment view in android studio

這個問題似乎只出現添加片段到我的活動佈局後(見上面的代碼和佈局)。我應該如何解決這個問題?

編輯:我不明白爲什麼我收到了投票。至少您可以留下評論,解釋問題的錯誤。

+0

你的活動佈局是什麼樣子? – dhiku

+0

@dhiku它是在問題「這是我的活動佈局:」... –

+0

爲什麼-1?這是一個合法的問題! –

回答

1

如何將您的FrameLayout寬度設置爲match_parent! 編輯: 爲ConstraintLayout,替換當前FrameLayout有:

<FrameLayout 
    android:id="@+id/contactInfoFragmentContainer" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent"> 

並確保您在XML根佈局聲明有xmlns:app="http://schemas.android.com/apk/res-auto"

+0

Android工作室不讓我。當我將它設置爲match_parent時,它將其更改爲368p –

+1

如果FrameLayout位於ConstraintLayout內,則將前者的寬度設置爲0,並將屬性layout_constraintLeft_toLeftOf和layout_constraintRight_toRightOf設置爲parent。 – Nithin

+0

我做到了,現在屏幕是空白的 –

相關問題