在Android中左右对齐文本

时间:2014-10-16 20:13:03

标签: java android alignment android-tablelayout

我目前在Android中对齐文字(左和右)有问题... 我想制作一张看似格式如下的收据。

Nasi Putih RM 1.00

Nasi Tomato RM 1.20

Nasi Hujan Panas RM 1.50

总价格:RM 3.70

以下是我的代码:

      if(nsPutih==true)
      {

          tvOutput1.setText("Nasi Putih RM 1.00");
          tvOutput1.setVisibility(View.VISIBLE);

          price+=1;
      }
      else
      {
          tvOutput1.setVisibility(View.GONE);
      }

      if (nsTomato==true)
      {
          tvOutput2.setText("Nasi Tomato RM 1.20");
          tvOutput2.setVisibility(View.VISIBLE);
          price+=1.2;
      }
      else
      {
          tvOutput2.setVisibility(View.GONE);
      }

      if(nsHjanPanas==true)
      {
          tvOutput3.setText("Nasi Hujan Panas RM 1.50");
          tvOutput3.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput3.setVisibility(View.GONE);
      }

我希望收据就像IN A TABLE一样非常对齐(方式是,所有那些Nasi元素都对齐,价格是正确对齐的。)请帮助我!

以下是xml的代码。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textB"
    android:layout_below="@+id/textB"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:textAppearance="?android:attr/textAppearanceMedium" />



   </LinearLayout>


   </ScrollView></RelativeLayout>

2 个答案:

答案 0 :(得分:0)

你是XML,你需要连续添加两个textview,一个用于Nasi元素,另一个用于价格元素。

尝试这样的事情,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="40dp" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal"
                android:weightSum="2" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal"
                android:weightSum="2" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal"
                android:weightSum="2" >

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

并更改您的java代码以匹配XML中设置的小部件的名称。如下所示,只需注意并在需要时进行必要的更改。

if(nsPutih==true)
      {

          tvOutput1.setText("Nasi Putih");
          tvOutput2.setText("RM 1.00");
          tvOutput1.setVisibility(View.VISIBLE);
          tvOutput2.setVisibility(View.VISIBLE);

          price+=1;
      }
      else
      {
          tvOutput1.setVisibility(View.GONE);
          tvOutput2.setVisibility(View.GONE);
      }

      if (nsTomato==true)
      {
          tvOutput3.setText("Nasi Tomato");
          tvOutput3.setVisibility(View.VISIBLE);
          tvOutput4.setText("RM 1.20");
          tvOutput4.setVisibility(View.VISIBLE);

          price+=1.2;
      }
      else
      {
          tvOutput3.setVisibility(View.GONE);
          tvOutput4.setVisibility(View.GONE);
      }

      if(nsHjanPanas==true)
      {
          tvOutput5.setText("Nasi Hujan Panas");
          tvOutput5.setVisibility(View.VISIBLE);
          tvOutput6.setText("RM 1.50");
          tvOutput6.setVisibility(View.VISIBLE)

          price+=1.5;
      }
      else
      {
          tvOutput5.setVisibility(View.GONE);
          tvOutput6.setVisibility(View.GONE);
      }

我希望它有所帮助。仍然对代码有疑问,请在下面评论。

答案 1 :(得分:0)

codePG的answer不会对齐右边的项目。这将:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"/>
</LinearLayout>