我正在尝试使用我的布局文件放置TextViews和Buttons行。看起来应该是这样的:
Row 1: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]
Row 2: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]
Row 3: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]
Row 4: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]
Row 5: [ TextView ( ~2/3 of the screen ) ] [ Button (~1/3 of the screen) ]
.....
我对如何做到这一点很困惑。我可以使用TableLayout或RelativeLayouts吗?有人可以给我看一些示例代码,因为我不知道如何使用XML。非常感谢你的帮助!
答案 0 :(得分:1)
是的,你可以用LinearLayout - android:weightSum做到这一点,这是一行的例子,
<?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="wrap_content"
android:weightSum="0.1" >
<TextView
android:text="left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33" />
<Button
android:text="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.67" />
</LinearLayout>
编辑: 我看到你不明白如何使用它, 假设你有两个布局,(main.xml和list_template.xml)
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout"
>
</LinearLayout>
</ScrollView>
<强> list_template.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="wrap_content"
android:weightSum="100" >
<TextView
android:text="left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="33"
android:id="@+id/list_textView" />
<Button
android:text="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="67"
android:id="@+id/list_button" />
</LinearLayout>
list_template是一行列表的布局模板,我们会给它充气。怎么样?
public class StackoverflowActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
LayoutInflater li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 5; i++){
View tempView = li.inflate(R.layout.list_template, null);
TextView list_textView = (TextView) tempView.findViewById(R.id.list_textView);
list_textView.setText("TextView"+i);
Button list_button = (Button) tempView.findViewById(R.id.list_button);
list_button.setText("Button"+i);
list_button.setId(i);
list_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Button clicked
Toast.makeText(StackoverflowActivity.this, "Button" + v.getId() + " clicked", Toast.LENGTH_SHORT).show();
}
});
mainLayout.addView(tempView);
}
}
}