2017-12-03 11 views
-1

"Hello World"のテキストビューが表示されません。 要素を取り込むlistviewは、すべて表示されます。wrap_contentの高さを持つTextViewは、その下のListViewで覆い隠されています

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    tools:context="lister.quantumproductions.com.lister.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     android:layout_above="@+id/recipe_list_view"></TextView> 

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

</RelativeLayout> 



public class MainActivity extends AppCompatActivity { 
    private ListView mListView; 

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

     loadListView(); 
    } 

    private void loadListView() { 
     mListView = findViewById(R.id.recipe_list_view); 
     final ArrayList<Recipe> recipes = Recipe.getRecipesFromFile("recipes.json", this); 

     RecipeAdapter a = new RecipeAdapter(this, recipes); 
     mListView.setAdapter(a); 
    } 
} 

答えて

1

あなたのListViewコントロールは、画面全体を埋め、その後、あなたはTextViewにはそれが表示されていないので、それは、画面の外に表示されます、リストビュー、超えるべきであると言います。

依存関係を逆転させる必要があります。

ListViewの上にあるTextViewの代わりに、ListViewがTextViewの下にある必要があります。

1

RelativeLayoutは、match_parentの幅と高さを持つ場合、最後の項目が前の項目をカバーするようにします。順序を変更するか、LinearLayoutを代わりに使用してください。

0

あなたはそれが完璧に動作します垂直方向と親としてのLinearLayout使用することができ、この

<TextView 
    android:id="@+id/text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"></TextView> 

<ListView 
    android:layout_below="@id/text_view" 
    android:id="@+id/recipe_list_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"></ListView> 
1

してみてください: -

私はあなたのXMLコードを修正し、このコードを使用することが

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="lister.quantumproductions.com.lister.MainActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:layout_above="@+id/recipe_list_view"></TextView> 

<ListView 
    android:id="@+id/recipe_list_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"></ListView> 
</LinearLayout> 
の作品を

これが動作し、textviewとlistviewの両方が表示されます。あなたが唯一のちょうどリストビューを作る相対的なレイアウトを使用したい包み

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
tools:context="lister.quantumproductions.com.lister.MainActivity"> 

<TextView 
    android:id="@+id/recipe_text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!"></TextView> 

<ListView 
    android:id="@+id/recipe_list_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/recipe_text_view"></ListView> 

</RelativeLayout> 
をwrap_content
関連する問題