我需要将这三个文本和一个图像一个接一个地水平放置。怎么办?
我的代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_placeorder,
container, false);
ScrollView scrl=new ScrollView(getActivity());
final LinearLayout ll=new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
scrl.addView(ll);
for(int i=0;i<3;i++) {
// TODO Auto-generated method stub
TextView time=new TextView(getActivity());
time.setText("07.23"+i);
ll.addView(time);
TextView orderId=new TextView(getActivity());
orderId.setText("ORDER:03987"+i);
ll.addView(orderId);
TextView dollarPrice = new TextView(getActivity());
dollarPrice.setText("$39.55"+i);
ll.addView(dollarPrice);
ImageView image = new ImageView(getActivity());
image.setImageResource(R.drawable.arrow_right);
ll.addView(image);
}
屏幕截图如下:
答案 0 :(得分:2)
您不需要动态创建所有视图。只需为项目创建xml并在循环内膨胀它。像..
<强> MainActivity.java 强>
public class MainActivity extends Activity {
private LinearLayout layoutmain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutmain = (LinearLayout) findViewById(R.id.layoutmain);
for (int i = 0; i <= 3; i++) {
View viewToLoad = LayoutInflater.from(MainActivity.this).inflate(
R.layout.row, null);
((LinearLayout) findViewById(R.id.layoutmain)).addView(viewToLoad);
}
TextView txt = new TextView(MainActivity.this);
txt.setText("Ship");
((LinearLayout) findViewById(R.id.layoutmain)).addView(txt);
for (int i = 0; i <= 3; i++) {
View viewToLoad = LayoutInflater.from(MainActivity.this).inflate(
R.layout.row, null);
((LinearLayout) findViewById(R.id.layoutmain)).addView(viewToLoad);
}
}
}
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="PROCESSED" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/layoutmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
<强> row.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtfirst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="07.23" />
<TextView
android:id="@+id/txtsecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ORDER:0378945" />
<TextView
android:id="@+id/txtthird" android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="$36.55" />
</LinearLayout>
输出:
答案 1 :(得分:1)
您可以使用方向为Horizontal的另一个LinearLayout,并将其设置为垂直LinearLayout。这样你的卷轴就不会受到影响。
这样的事情:
View view = inflater.inflate(R.layout.fragment_placeorder,
container, false);
ScrollView scrl=new ScrollView(getActivity());
LinearLayout ll=new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
for(int i=0;i<3;i++) {
// TODO Auto-generated method stub
LinearLayout l2=new LinearLayout(getActivity());
l2.setOrientation(LinearLayout.HORIZONTAL);
TextView time=new TextView(getActivity());
time.setText("07.23"+i);
l2.addView(time);
TextView orderId=new TextView(getActivity());
orderId.setText("ORDER:03987"+i);
l2.addView(orderId);
TextView dollarPrice = new TextView(getActivity());
dollarPrice.setText("$39.55"+i);
l2.addView(dollarPrice);
ImageView image = new ImageView(getActivity());
image.setImageResource(R.drawable.arrow_right);
l2.addView(image);
l1.addView(l2);
}
scrl.addView(ll);
这将在主外部布局中添加一个新的线性布局,其中3个textview和imageview在一行中,它将是垂直的,可滚动的。
希望这有帮助。
答案 2 :(得分:0)
<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"
android:text="TextView"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
并在您的班级中将此布局设置为水平或垂直,如下所示:
LinearLayout m =(LinearLayout) findViewById(R.id.LinearLayout1);
m.setOrientation(LinearLayout.HORIZONTAL);