【问题标题】:Right and left align in a single ListView在单个 ListView 中左右对齐
【发布时间】:2013-02-22 01:07:07
【问题描述】:

我正在动态设置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;
}

【问题讨论】:

  • 在 XML 中执行!容易得多:),请为您的列表视图行提供您的 xml
  • 你有自定义行xml吗?
  • 不,我不...什么是自定义行?
  • 简单示例here。你需要使用SimpleAdapter
  • 简而言之:创建一个代表每一行的新 xml 布局,例如,如果您连续有 3 个 TextView,您将在新的 xml 布局文件中添加它们。他们每个人都会有一个id。剩下的工作由 SimpleAdapter 完成。

标签: java android xml listview


【解决方案1】:

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

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

主要活动:

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>

几个小时后,我将添加一些 cmets。 结果在这里

【讨论】:

  • 这完全出乎我的意料......哈哈
  • 嗨@ChadD.Bingham 抱歉重播晚了,我很忙。第一个我无法访问它说是私人粘贴的pastebin,将其公开。第二,我将编辑我的答案以添加一些解释我的代码的 cmets。第三,我的结果与您想要达到的结果相似吗?如果是的话,我会一步一步地帮助你。
  • 我会给你一个答案。检查您的电子邮件
【解决方案2】:

如果我没记错的话,您必须扩展 BaseAdapter 类,以便为您的 ListView 提供一个自定义适配器,如果不创建自定义适配器,就无法将两个数组项添加到 ListView。

这是tutorial.

并不像看起来那么难。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2011-01-19
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多