2017-02-27 55 views
0

当我在我的布局文件中声明一个PreferenceFragment这样片段占用更多的空间比它的内容将需要

<LinearLayout 
     android:id="@+id/webViewLayout" 

     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     android:orientation="horizontal"> 

     <fragment 
      android:id="@+id/toggleView" 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:name="path_to_fragment.ToggleView_PrefFragment" /> 

     <WebView 
      android:id="@+id/webView" 

      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

</LinearLayout> 

only the PreferenceFragment appears on the screen因为所有的空间正在被片段占领的WebView高度等于零( )。

但是为什么呢?我声明了片段的高度“WRAP_CONTENT”,所以应该只占用它需要的空间......

任何帮助表示赞赏:)

编辑:

ToggleView_PrefFragment:

package fragment; 

import android.os.Bundle; 
import android.preference.PreferenceFragment; 

import R; 

public final class ToggleView_PrefFragment extends PreferenceFragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     addPreferencesFromResource(R.xml.toggle_view); 

    } 
} 

toggle_view.xml:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 

    <CheckBoxPreference 
     android:defaultValue="false" 
     android:summary="Summary" 
     android:title="Title" 
    /> 

</PreferenceScreen> 
+0

片段的视图,没有设置为'''match_parent'''的高度? – danypata

+0

它不应该设置为match_parent。我的问题是,它的行为就像它将设置为match_parent。 – Makk0

+1

我建议检查Fragment的视图(如果设置为match_parent的话,我假设你创建了一个具有用于布局的xml文件的片段)没有将高度设置为''match_parent'''那么这是预期的行为。 – danypata

回答

0

您的WebView不可见,因为您的根布局是水平LinearLayout。 您的PreferenceFragment的宽度为match_parent,因此WebView没有空间。 此外,您使WebView匹配所有可用空间使用重量。

我假设你想拥有PreferenceFragment顶部与wrap_content高度。并且在PreferenceFragment以下有WebView,它匹配所有其余的垂直空间。

在这种情况下,你应该这样做:

<LinearLayout 
     android:id="@+id/webViewLayout" 

     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     android:orientation="vertical"> 

     <fragment 
      android:id="@+id/toggleView" 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:name="path_to_fragment.ToggleView_PrefFragment" /> 

     <WebView 
      android:id="@+id/webView" 

      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

</LinearLayout> 

变化LinearLayout定向垂直。 为您的WebView开关宽度和高度选项。并将宽度更改为match_parent。

希望它有帮助

+0

你先生,是上帝!非常感谢你 :) – Makk0

相关问题