2017-12-03 8 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는 전체 화면을 채우고, 다음은 텍스트 뷰가 화면 바깥에 표시하게 ListView에, 이상이어야합니다라고, 그래서 그것을 볼 수 없습니다.

종속성을 뒤집어 야합니다.

그래서 ListView 위에있는 TextView 대신 ListView가 TextView 아래에 있어야합니다.

1

RelativeLayout은 match_parent 너비와 높이가 있으면 마지막 항목이 이전 항목을 덮도록합니다. 순서를 변경하거나 대신에 LinearLayout을 사용하십시오.

0

노력이

<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
당신이 완벽하게 작동 수직 방향으로 부모로있는 LinearLayout를 사용할 수 있습니다

: -

내가 당신의 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