Java 在Android中左右对齐文本
我目前在Android中对文本左对齐和右对齐有问题。。。 我想做一张如下格式的收据 Nasi PutihRM 1.00 鼻托膜1.20 Nasi Hujan PanasRM 1.50 总价:3.70马来西亚令吉 下面是我的代码:Java 在Android中左右对齐文本,java,android,alignment,android-tablelayout,Java,Android,Alignment,Android Tablelayout,我目前在Android中对文本左对齐和右对齐有问题。。。 我想做一张如下格式的收据 Nasi PutihRM 1.00 鼻托膜1.20 Nasi Hujan PanasRM 1.50 总价:3.70马来西亚令吉 下面是我的代码: if(nsPutih==true) { tvOutput1.setText("Nasi Putih RM 1.00"); tvOutput1.setVisibility(View.VISIBLE);
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);
}
我希望收据像一张表格一样,排列整齐,所有的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>
使用XML时,需要在一行中添加两个textview,一个用于Nasi元素,另一个用于price元素 试试这样的
<?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>
我希望有帮助。对代码仍有疑问,请在下面进行评论。codePG不会对齐右侧的项目。这将:
<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>
对于价格,您必须使用不同的文本视图。将文本视图与列对齐。第一个用于文本,第二个用于Rs。您没有指定任何layoutParams。用xml创建布局要容易得多。您可以使用GridView(我会这么做),但也可以使用TableView或线性布局的组合,使用垂直线性布局和一些嵌套的水平线性布局。如果你有一个用XML定义的布局,那么请发布它。。。名称和成本分别。。。要更精确地使用表格布局。。。
<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>