我在 main.xml 文件中创建了按钮并在活动文件中对其进行了初始化,但我不确定如何使用 xml 中定义的布局让按钮出现在屏幕上。谁能帮我这个?
public class MyTasteActivity extends Activity implements View.OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
Button collectionBtn = (Button) findViewById(R.id.collectionBtn);
}
public void collectionBtn(View v) {
Toast.makeText(getApplicationContext(), "Collection was pressed", Toast.LENGTH_LONG).show();
}
public void onClick(View v) {}
}
XML 文件:
<LinearLayout
android:layout_width="310px"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/collectionBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="collectionBtn"
android:text="Collection" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<EditText
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="300px" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
</LinearLayout>
</ScrollView>