ListView中的项目(Android)

时间:2015-04-19 15:24:27

标签: android listview

我制作了一个简单的ListView,它工作得很好,我仍然唯一需要在ListView中心显示项目。

这是我的explication.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Results"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#e33939"
    android:textSize="40dp" />

<ListView
    android:id="@+id/list"
    android:layout_width="180dp"
    android:layout_height="430dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:gravity="center"
    android:background="#e33939" >
</ListView>
</RelativeLayout>

这是我的活动Learn.java:

public class Learn extends  Activity {
ListView listView ;

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

    // Get ListView object from xml
    listView = (ListView) findViewById(R.id.list);

    // Defined Array values to show in ListView
    String[] values = new String[] { 
        " a = أ - ى - ا ", " b = ب ", " c = س ", " d = د ", " f = ف ", " h = ه " , " i = ئ", " j = ج",
        " k = ك", " l = ل", " m = م ", " n = ن", " o = ؤ ", " p = ب", " q = ك", " r = ر",  " s = س",
        " t = ت - ث - ة ", " v = ف " , " w = و" , " x = اكس" , " y = ي", " z = ز ", " sh = ش" , 
        " ء - ؤ = 2  " ,  " ع = 3  ", " غ = 3′ " , " ذ = 4 ", " خ = 5 ", " ط = 6  ", " ظ = ‘6 ",
        " ح = 7 ", " ق = 8  ", "ص =  9 " , " ض = ‘9  "

                                    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter); 

    // ListView Item Click Listener
    listView.setOnItemClickListener(new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {

           // ListView Clicked item index
           int itemPosition     = position;

           // ListView Clicked item value
           String  itemValue    = (String) listView.getItemAtPosition(position);

            // Show Alert 
            Toast.makeText(getApplicationContext(),""+"" +itemValue , Toast.LENGTH_LONG)
              .show();
          }
     }); 
}
}

项目显示在sceean的左侧。我尝试了多项但没有结果。谢谢

2 个答案:

答案 0 :(得分:3)

你必须改变行xml。如下所示

row.xml

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >
        <TextView
            android:id="@id/textItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </LinearLayout>

必须仅更新一行后

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      R.layout.row, R.id.textItem, values);

祝你好运

答案 1 :(得分:0)

在items.xml文件中执行此操作

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true">


        </LinearLayout>
    </RelativeLayout>

这将在列表视图的中心创建项目。