2017-05-27 174 views
0

我想要一个简单的设置,其中我有一个2列的布局,并且在每个布局中,我将嵌套其他布局和控件。正如我的根布局我使用GridLayout,至今它进行得很顺利:ListView打破布局

Correct Layout

我加ListView给我的第一主柱的那一刻,它忽略了第一列的范围。我曾尝试everythring我知道,甚至嵌套其他布局内ListView但我总是落得这样的:

List Problem

这里是我的布局XML至今:

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout 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:columnCount="2" 
tools:context="com.example.mycoffeeshops.MainActivity"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_column="0" 
    android:layout_columnWeight="1" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/cofeeShopsTitleLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:width="0dp" 
     android:gravity="center_horizontal" 
     android:text="@string/coffee_shops_title" 
     android:textSize="@dimen/coffee_shops_title" 
     android:textStyle="bold" /> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:columnCount="3"> 

     <TextView 
      android:id="@+id/shopLabelLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:width="0dp" 
      android:text="@string/shop_label" /> 

     <EditText 
      android:id="@+id/shopLabelEditText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="3" 
      android:width="0dp" 
      android:ems="10" 
      android:hint="@string/shop_label_hint" 
      android:inputType="textPersonName" /> 

     <ImageButton 
      android:id="@+id/saveCoffeeShopImageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="2" 
      app:srcCompat="@android:drawable/ic_menu_save" /> 
    </GridLayout> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:columnCount="3"> 

     <TextView 
      android:id="@+id/descriptionLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:width="0dp" 
      android:text="@string/shop_label" /> 

     <EditText 
      android:id="@+id/descriptionEditText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="3" 
      android:width="0dp" 
      android:ems="10" 
      android:hint="@string/description_hint" 
      android:inputType="textPersonName" /> 

     <Space 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="2" 
      android:layout_columnWeight="0.5" /> 

    </GridLayout> 

    <TextView 
     android:id="@+id/coffeeShopListLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:width="0dp" 
     android:gravity="center_horizontal" 
     android:paddingBottom="@dimen/coffee_shop_list_label_padding_bottom" 
     android:paddingTop="@dimen/coffee_shop_list_label_padding_top" 
     android:text="@string/coffee_shops_list_label" 
     android:textSize="@dimen/coffee_shops_list_label_text_size" 
     android:textStyle="bold" /> 

    <ListView 
     android:id="@+id/shopListListView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_column="1" 
    android:layout_columnWeight="1" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/detailedIformationTitleLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:width="0dp" 
     android:gravity="center_horizontal" 
     android:text="@string/detailed_information_title" 
     android:textSize="@dimen/coffee_shops_title" /> 
</LinearLayout> 

+0

你可以尝试两台分离成两个不同的片段,并动态加载根据屏幕大小 –

+0

对于这个pariticular一个我不能使用片段 –

回答

2

这是你想要的代码。这项工作很好我

<?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="horizontal" 
android:weightSum="100" 
tools:context="com.example.mycoffeeshops.MainActivity"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="50" 
    android:orientation="vertical"> 

    <Space 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <TextView 
     android:id="@+id/cofeeShopsTitleLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:width="0dp" 
     android:gravity="center_horizontal" 
     android:text="@string/coffee_shops_title" 
     android:textSize="@dimen/coffee_shops_title" 
     android:textStyle="bold" /> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:columnCount="3"> 

     <TextView 
      android:id="@+id/shopLabelLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:width="0dp" 
      android:text="@string/shop_label" /> 

     <EditText 
      android:id="@+id/shopLabelEditText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="3" 
      android:width="0dp" 
      android:ems="10" 
      android:hint="@string/shop_label_hint" 
      android:inputType="textPersonName" /> 

     <ImageButton 
      android:id="@+id/saveCoffeeShopImageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="2" 
      app:srcCompat="@android:drawable/ic_menu_save" /> 
    </GridLayout> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:columnCount="3"> 

     <TextView 
      android:id="@+id/descriptionLabel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnWeight="1" 
      android:width="0dp" 
      android:text="@string/shop_label" /> 

     <EditText 
      android:id="@+id/descriptionEditText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnWeight="3" 
      android:width="0dp" 
      android:ems="10" 
      android:hint="@string/description_hint" 
      android:inputType="textPersonName" /> 

    </GridLayout> 

    <TextView 
     android:id="@+id/coffeeShopListLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:width="0dp" 
     android:gravity="center_horizontal" 
     android:paddingBottom="@dimen/coffee_shop_list_label_padding_bottom" 
     android:paddingTop="@dimen/coffee_shop_list_label_padding_top" 
     android:text="@string/coffee_shops_list_label" 
     android:textSize="@dimen/coffee_shops_list_label_text_size" 
     android:textStyle="bold" /> 

     <ListView 
      android:id="@+id/shopListListView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="50" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/detailedIformationTitleLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:width="0dp" 
     android:gravity="center_horizontal" 
     android:text="@string/detailed_information_title" 
     android:textSize="@dimen/coffee_shops_title" /> 
</LinearLayout> 
</LinearLayout> 

但尝试使用ConstraintLayout。通过ConstraintLayout,您可以轻松实现这种布局。

教程: - https://developer.android.com/training/constraint-layout/index.html