可以在textview中替换行颜色吗?

时间:2016-01-16 05:12:00

标签: android android-layout listview arraylist background-color

我所做的是创建了一个带有3个textview(tablelayout)的按钮,使其看起来像一个记录显示。但是,我遇到的一个问题是我想要替换行颜色,我发现我只能用listview来做。有没有任何方法可以在没有listview的情况下使用我当前的代码进行操作?

SortName(Button)

Date(TextView)    Name(TextView)  URL(TextView)
12/01/2015        Google          www.google.com
02/11/2015        Yahoo           www.yahoo.com

activity_record.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#b7010101">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3"
    android:id="@+id/linearLayout">
<Button
    android:id="@+id/SortName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sort By Name" />
</LinearLayout>
<ScrollView
    android:id="@+id/layout"
    android:layout_height="match_parent"
    android:scrollbars="horizontal|vertical"
    android:layout_width="match_parent"
    android:layout_marginTop="3dip"
    android:scrollbarStyle="outsideInset"
    android:fillViewport="true">
<HorizontalScrollView
    android:id="@+id/horizontalView"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal|vertical"
    android:layout_width="wrap_content"
    android:layout_marginTop="3dip">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tlGridTable" >
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <TextView
                android:id="@+id/textView4" android:text="Date"
                android:layout_weight="1" android:background="#cac9c9"
                android:textColor="#000000"
                android:padding="10dip" android:gravity="center"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/textView2" android:text="Name"
                android:layout_weight="1" android:background="#d3d3d3"
                android:textColor="#000000"
                android:padding="10dip" android:gravity="center"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/textView3" android:text="URL"
                android:layout_weight="1" android:background="#cac9c9"
                android:textColor="#000000"
                android:padding="10dip" android:gravity="center"
                android:textStyle="bold" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
    <TextView
                android:id="@+id/textView5"
                android:layout_weight="1" android:background="@drawable/back"
                android:textColor="#000000"
                android:padding="6dip" android:gravity="left"/>

            <TextView
                android:id="@+id/textView6"
                android:layout_weight="1"       android:background="@drawable/back"
                android:textColor="#000000"
                android:padding="6dip" android:gravity="left"/>

            <TextView
                android:id="@+id/textView7"
                android:layout_weight="1" android:background="@drawable/back"
                android:textColor="#000000"
                android:padding="6dip" android:gravity="left"/>

        </TableRow>
    </TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>

Record.java

public class Record extends AppCompatActivity {
Button SortName;
TextView view,view2,view3;
ArrayList<HistoryDetails> userList1 = new ArrayList<>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    view = (TextView) findViewById(R.id.textView5);
    view2 = (TextView) findViewById(R.id.textView6);
    view3 = (TextView) findViewById(R.id.textView7);

    SortName = (Button)findViewById(R.id.SortName);

    SortName.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            view.setText("");
            view2.setText("");
            view3.setText("");

            Collections.sort(userList1, new Comparator<HistoryDetails>() {
                @Override
                public int compare(HistoryDetails first, HistoryDetails second) {
                    return first.name.compareToIgnoreCase(second.name);
                }
            });

            for (HistoryDetails obj : userList1) {
                view.append(getDate(obj.date, "dd/MM/yyyy   HH:mm:ss") + "\n");

                if (obj.name.length() > 30)
                    view2.append(obj.name.substring(0, 30) + "\n");
                else
                    view2.append(obj.name + "\n");

                if (obj.url.length() > 30)
                    view3.append(obj.url.substring(0, 30) + "\n");
                else
                    view3.append(obj.url + "\n");
            }
        }
    });
}

HistoryDe​​tails.java

public class HistoryDetails {
    public HistoryDetails(Long date, String name, String url) 
      {
        this.date = date;
        this.name = name;
        this.url = url;
      }
    public long date = 0;
    public String name = "";
    public String url = "";
}

2 个答案:

答案 0 :(得分:2)

  

有没有任何方法可以在没有listview的情况下使用我的当前   码?

使用TableLayout并使用getChildAtTableRow设置备用行颜色:

TableLayout tableLayout=(TableLayout) findViewById(R.id. tlGridTable);
for (int count = 0; count < tableLayout.getChildCount(); count++) {
  View view=tableLayout.getChildAt(count);
  // access TextViews from Row
  View viewText1 = view.getChildAt(0);
  View viewText2 = view.getChildAt(1);
  // access others in same way...
  if (count % 2 == 1) {
     view.setBackgroundColor(Color.BLUE);  
     // set color for TextView's
     viewText1.setBackgroundColor(Color.BLACK); 

   }else{
     view.setBackgroundColor(Color.GREEN);  

    // set color for TextView's
     viewText1.setBackgroundColor(Color.GRAY); 
   }
}

我们还可以使用textView5, textView6, textView7访问view,为每行中的TextView设置不同的颜色。

答案 1 :(得分:0)

拥有一个TextView数组很容易

int anternate=0;
for(int i=0; i<lenght; i++)
{
if(alternate==1) textview[i].setBackgroundColor(R.color.GRAY);//choose your color
else textview[i].setBackgroundColor(0x00000000);//transparent
alternate++;
alternate=alternate & 1;
}