2017-04-26 43 views
0

我是Android的初學者。我想顯示一個包含數字的自定義列表視圖。我自定義了我的List item高度尺寸(android:layout_height =「50dp」)。但是當我運行這個應用程序時,它顯示了大部分沒有給出的尺寸。我不知道如何解決這個問題。任何人都可以幫我嗎?如何在android中自定義列表項的高度?

activity_topic_list.xml

<?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="vertical" 
    tools:context="com.example.yuvi.secrectsforhappylife.TopicList"> 

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

titledesign.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="@drawable/storylist"> 

    <TextView 
     android:id="@+id/topictitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/title" 
     android:textSize="25sp" 
     android:textStyle="bold" 
     android:textColor="@android:color/background_light" /> 
</RelativeLayout> 

TopicList.java

public class TopicList extends AppCompatActivity { 

     String topic\[\]={"one","two","three","four","five","six","seven","eight","nine","ten"}; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_topic_list); 

      ListView listView=(ListView)findViewById(R.id.topiclist); 

      CustomAdapter customAdapter=new CustomAdapter(); 
      listView.setAdapter(customAdapter); 

     } 

     class CustomAdapter extends BaseAdapter 
     { 

      @Override 
      public int getCount() { 
       return topic.length; 
      } 

      @Override 
      public Object getItem(int position) { 
       return null; 
      } 

      @Override 
      public long getItemId(int position) { 
       return 0; 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       convertView=getLayoutInflater().inflate(R.layout.titledesign,null); 
       TextView title=(TextView)convertView.findViewById(R.id.topictitle); 
       title.setText(topic\[position\]); 
       return convertView; 
      } 
     } 
    } 

回答

0

嘗試以下在您的列表視圖代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/storylist"> 

,並進行更改也喜歡下面

<ListView 
    android:id="@+id/topiclist" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
相關問題