在一个单一的ListView左,右对齐 [英] Right and left align in a single ListView
问题描述
我设置一个的ListView
动态。我将有两对,我会从 parse.com
接收每行显示的东西。我想留下的第一件事,对准和蓝色,与第二件事右对齐和红色。我有两个数组, firstArray
和 secondArray
。我发现很多方法可以做到这一点,但没有动态。我应该如何去这样做,我也可以在 XML
?
更新
这是我的 XML
以后的教程帖由@ T0S
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:填充=2DP
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
<的TextView
机器人:ID =@ + ID /列1
机器人:layout_width =50dip
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字颜色=#0000FF
机器人:重力=左
/>
<的TextView
机器人:ID =@ + ID /中心
机器人:文本=VS
机器人:TEXTSIZE =15sp
机器人:layout_width =50dip
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字颜色=#FFFFFF
机器人:重力=中心
/>
<的TextView
机器人:ID =@ + ID /列2
机器人:layout_width =50dip
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字颜色=#FF0000
机器人:重力=右
/>
< / LinearLayout中>
和我的Java
的ListView列表=(ListView控件)findViewById(R.id.mylist);
ArrayAdapter<字符串> listAdapter =新的ArrayAdapter<字符串>(MainActivity.this,R.layout.mylist,namesArray);
list.setAdapter(listAdapter);
//找到我的对象过解析,并且调用此方法
私人字符串显示(对象名称,对象记录,
ArrayAdapter listAdapter){
字符串fightCard =(名称)的ToString();
namesArray.add((名称)的ToString()++(记录)的ToString());
返回fightCard;
}
那么我的code是一个烂摊子。 这里检查教程是 [非常简单]
在这里好了乍得检查code:
主要活动:
包org.example.chad;
进口的java.util.ArrayList;
进口android.app.ListActivity;
进口android.os.Bundle;
公共类MainActivity扩展ListActivity {
私人的ArrayList< DataItem的>数据=新的ArrayList< DataItem的>();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//创建对象
DataItem的OBJ1 =新的DataItem(名1,RECORD1);
DataItem的OBJ2 =新的DataItem(名2,RECORD2);
DataItem的obj3 =新的DataItem(NAME3,RECORD3);
DataItem的OBJ4 =新的DataItem(名称4,RECORD4);
DataItem的obj5 =新的DataItem(name5,record5);
//添加到ArrayList中
data.add(OBJ1);
data.add(OBJ2);
data.add(obj3);
data.add(OBJ4);
data.add(obj5);
CustomAdapter适配器=新CustomAdapter(这一点,R.layout.row,数据);
setListAdapter(适配器);
}
}
的main.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
<的ListView
机器人:ID =@机器人:ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
< / RelativeLayout的>
的DataItem
包org.example.chad;
公共类的DataItem {
私人字符串名称;
私人字符串记录;
公众的DataItem(){
}
公众的DataItem(字符串N,串R){
this.name = N;
this.record = R;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公共字符串getrecord(){
返回记录;
}
公共无效setrecord(字符串记录){
this.record =记录;
}
}
CustomAdapter:
包org.example.chad;
进口的java.util.ArrayList;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;
公共类CustomAdapter扩展ArrayAdapter< DataItem的> {
私人的ArrayList< DataItem的>对象;
公共CustomAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< DataItem的>对象){
超(背景下,textViewResourceId,对象);
this.objects =物体;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
视图V = convertView;
如果(V == NULL){
LayoutInflater充气=(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
V = inflater.inflate(R.layout.row,NULL);
}
DataItem的I = objects.get(位置);
如果(我!= NULL){
TextView的nameView =(TextView中)v.findViewById(R.id.name);
TextView的recordView =(TextView中)v.findViewById(R.id.record);
如果(nameView!= NULL){
nameView.setText(i.getname());
}
如果(recordView!= NULL){
recordView.setText(i.getrecord());
}
}
返回伏;
}
}
和 row.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
<的TextView
机器人:ID =@ + ID /名称
机器人:layout_width =100SP
机器人:layout_height =20SP
机器人:TEXTSIZE =16SP
机器人:layout_gravity =左| center_vertical
机器人:layout_marginLeft =10SP/>
<的TextView
机器人:ID =@ + ID /记录
机器人:layout_width =100SP
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =10SP
机器人:layout_gravity =右
机器人:TEXTSIZE =16SP/>
< / LinearLayout中>
在一两个小时,我会补充一些意见。 其结果是在这里
I am setting a listView
dynamically. I will have two things shown on each line that I will be receiving from parse.com
. I want the first thing to be left aligned and blue, with the second thing right aligned and red. I will have two arrays, firstArray
, and secondArray
. I found lots of ways to do this, but none dynamically. How should I go about doing this, or I can do it in xml
?
Updated
This is my xml
after that tutorial posted by @t0s
<?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>
and my 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;
}
Well my code is a mess. Check the tutorial here is [very simple]
Ok Chad check here the code :
Main 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);
}
}
main.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>
DataItem :
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;
}
}
CustomAdapter :
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;
}
}
and 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>
In a couple of hours I will add some comments. The result is here
这篇关于在一个单一的ListView左,右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!