gpt4 book ai didi

android - ListView 中的项目居中 (Android)

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:42 26 4
gpt4 key购买 nike

我已经制作了一个简单的 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>

这是我的 Activity 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();
}
});
}
}

项目显示在屏幕的左侧。我试过很多东西但没有结果。谢谢

最佳答案

您必须更改行 xml。对于下面的例子

行.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);

祝你好运

关于android - ListView 中的项目居中 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29731806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com