java - 在单个 ListView 中左右对齐

标签 java android xml listview

我正在动态设置一个listView。我将从 parse.com 接收的每一行显示两件事。我希望第一件事左对齐且为蓝色,第二件事右对齐且为红色。我将有两个数组,firstArraysecondArray。我找到了很多方法来做到这一点,但没有一个是动态的。我应该如何着手执行此操作,或者我可以在 xml 中执行此操作?

已更新

这是我在@t0s 发布的教程之后的 xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/column1"
    android:layout_width="50dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="#0000FF"
    android:gravity="left"
    />
    <TextView
    android:id="@+id/center"
    android:text="VS"
    android:textSize="15sp"
    android:layout_width="50dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="#FFFFFF"
    android:gravity="center"
   />
       <TextView
    android:id="@+id/column2"
    android:layout_width="50dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="#FF0000"
    android:gravity="right"
   />
    </LinearLayout>

和我的java

 ListView list = (ListView) findViewById(R.id.mylist);

                ArrayAdapter<String> listAdapter = new  ArrayAdapter<String>(MainActivity.this, R.layout.mylist, namesArray);
                list.setAdapter(listAdapter);

 //find my objects off parse, and call this method

private String display(Object name, Object record,
         ArrayAdapter listAdapter) {

    String fightCard = (name).toString();
    namesArray.add((name).toString() +" " + (record).toString());
    return fightCard;
}

最佳答案

嗯,我的代码一团糟。 查看这里的教程是[very simple]

好的乍得检查这里的代码:

主要 Activity :

package org.example.chad;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;

public class MainActivity extends ListActivity {


private ArrayList<DataItem> data = new ArrayList<DataItem>();

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

    //create Objects
    DataItem obj1 = new DataItem("name1", "record1");
    DataItem obj2 = new DataItem("name2", "record2");
    DataItem obj3 = new DataItem("name3", "record3");
    DataItem obj4 = new DataItem("name4", "record4");
    DataItem obj5 = new DataItem("name5", "record5");
    //add the to ArrayList
    data.add(obj1);
    data.add(obj2);
    data.add(obj3);
    data.add(obj4);
    data.add(obj5);


        CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data);
        setListAdapter(adapter);        

}

ma​​in.xml :

<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=".MainActivity" >

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

数据项:

package org.example.chad;

public class DataItem {
private String name;
private String record;

public DataItem(){

}

public DataItem(String n, String r ){
    this.name = n;
    this.record = r;
}

public String getname() {
    return name;
}

public void setname(String name) {
    this.name = name;
}

public String getrecord() {
    return record;
}

public void setrecord(String record) {
    this.record = record;
}

自定义适配器:

package org.example.chad;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<DataItem> {
   private ArrayList<DataItem> objects;

   public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem>     objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
}

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

  View v = convertView;
  if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.row, null);
    }


  DataItem i = objects.get(position);

if (i != null) {
    TextView nameView = (TextView) v.findViewById(R.id.name);
    TextView recordView = (TextView) v.findViewById(R.id.record);

    if (nameView != null){
        nameView.setText(i.getname());
    }
    if (recordView != null){
        recordView.setText(i.getrecord());
    }

}

return v;
  }
} 

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="horizontal" >


    <TextView
      android:id="@+id/name"
      android:layout_width="100sp"
      android:layout_height="20sp"
      android:textSize="16sp"
      android:layout_gravity="left|center_vertical"
      android:layout_marginLeft="10sp"/>

    <TextView
        android:id="@+id/record"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10sp"
        android:layout_gravity="right"
        android:textSize="16sp" />

</LinearLayout>

几个小时后我会添加一些评论。 结果在这里 enter image description here

关于java - 在单个 ListView 中左右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15015179/

相关文章:

java - 我们可以将 getter 和 setter 放在抽象类中吗?

java - 颠倒句子中的每个单词..变化不会出现

java - java中FileReader或FileWriter对象的flush方法调用

android - 如何使从应用程序导出的新联系人以可编辑的形式导出到电话簿

python - lxml:在给定位置插入标签

java - Spring MVC @RequestParam 和 @SessionAttribute

android - 表在带有 android 的 SQLite 中没有名为 "Column Name"的列

Android sherlock 库无效选项菜单问题

c++ - 从字符串流到字符串的转换删除了 '=' 个字符

sql-server - SSIS将具有不同类型列的表导出到平面文件中