Android:LinearLayout中Fragment和TextView之间的差距

时间:2014-02-13 14:56:24

标签: android android-layout android-fragments

我正在编写一个在 Nexus10 上运行的应用程序。我认为不重要。我使用 Android版本4.4.2

我正在尝试使用片段来显示一个TextViews的公共组,这组显示为使用Activity的{​​{1}}的前3个元素。

我没有在以下两个LinearLayout XML代码段之外进行任何格式化操作。

在第一个(父级)中,我有一个片段和两个文本视图。片段与LinearLayout之间存在巨大差距。如果我从片段标记中删除TextViewsandroid:layout_height="0dp"属性,则片段根本不会显示。有谁知道为什么我可能会在片段和它下面显示的两个TextView之间得到这个巨大差距?谢谢!

这是父级(android:layout_weight="1")级别布局。下面是Activity特定布局。

fragment

以下是特定于片段的布局。

<?xmlversion="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    androidrientation="vertical" >
    <fragment
        android:name="com.srcinc.drphilos.ChemicalNameAndC asNumberFragment"
        android:id="@+id/chemical_name_and_cas_number"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1"
    /> 
    <TextView
        android:id="@+id/healthEffectsSummaryLabel"
        android:textStyle="bold" 
        style="@android:style/TextAppearance.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
    />
    <TextView
        android:id="@+id/toxletText" 
        style="@android:style/TextAppearance.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    /> 
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

使用

android:layout_height="0dp" 
android:layout_weight="1"

你告诉片段增长并填满屏幕中的所有空白区域。

如果您想要的是TextViews出现在片段之后,那么您应该放置的参数是wrap_content:

<fragment
    android:name="com.srcinc.drphilos.ChemicalNameAndC asNumberFragment"
    android:id="@+id/chemical_name_and_cas_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
/>

答案 1 :(得分:0)

试试这个,

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidrientation="vertical" >
<fragment
    android:name="com.srcinc.drphilos.ChemicalNameAndC asNumberFragment"
    android:id="@+id/chemical_name_and_cas_number"
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:layout_weight="8"
/> 
<TextView
    android:id="@+id/healthEffectsSummaryLabel"
    android:textStyle="bold" 
    style="@android:style/TextAppearance.Medium"
    android:layout_width="wrap_content"
    android:layout_height="0dp" 
    android:layout_weight="1"
/>
<TextView
    android:id="@+id/toxletText" 
    style="@android:style/TextAppearance.Medium"
    android:layout_width="wrap_content"
    android:layout_height="0dp" 
    android:layout_weight="1"
/> 

这会将整个屏幕划分为8部分片段和1:1部分TextViews。如果这不是您正在寻找的,请告诉我。