在Android活动中看不到按钮活动
无论我尝试过,底部的按钮都看不到。顶部应该有一个TextView,下面有六个按钮。但不知何故,我看不到他们。我看了here,还有here,但他们没有帮助。请告诉我,这是什么问题?这里是我的布局:在Android活动中看不到按钮活动
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="17dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="74dp"
android:layout_marginTop="64dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/bttn_aux1"
android:onClick="is_clicked" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="@string/bttn_aux2"
android:onClick="can_clicked" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="@string/bttn_aux3"
android:onClick="must_clicked" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button3"
android:layout_below="@+id/button3"
android:text="@string/bttn_aux4"
android:onClick="do_clicked" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:text="@string/bttn_aux5"
android:onClick="will_clicked" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button5"
android:layout_below="@+id/button5"
android:text="@string/bttn_aux6"
android:onClick="has_clicked" />
</RelativeLayout>
编辑:另外这是我的活动:
public class AuxilaryVerbs extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auxilary);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRAMESSAGE_MAIN);
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(message);
setContentView(textView);
}
public void is_clicked(View view) {
}
public void can_clicked(View view) {
}
public void must_clicked(View view) {
}
public void do_clicked(View view) {
}
public void will_clicked(View view) {
}
public void has_clicked(View view) {
}
}
Eclipse自动xml生成器增加了很多额外的属性,你不需要和当你尝试手动编辑它会迷路。
首先将RelativeLaout替换为LinearLayout并使其垂直,这将使其更简单。然后删除所有layout_toRightOf,left,below等属性,并使其变得简单。然后你会看到你所有的按钮。但是,如果没有足够的空间在设备中显示所有内容,则必须在ScrollView中放置按钮,以便滚动并获取所有按钮。
我尝试更新XML(可能会有一些错别字)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="17dp"
android:text="TextView" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="74dp"
android:layout_marginTop="64dp"
android:text="@string/bttn_aux1"
android:onClick="is_clicked" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bttn_aux2"
android:onClick="can_clicked" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bttn_aux3"
android:onClick="must_clicked" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bttn_aux4"
android:onClick="do_clicked" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bttn_aux5"
android:onClick="will_clicked" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bttn_aux6"
android:onClick="has_clicked" />
</LinearLayout>
编辑 删除此。你只用这个textView设置你的整个视图。你已经设定了你的观点。
setContentView(textView);
您已使用以下行设置布局。你不应该再覆盖它。
setContentView(R.layout.activity_auxilary);
当您使用布局文件设置了内容视图时,您已经拥有了自己的文本视图。您可以使用以下行访问它。
TextView textView = (TextView) v.findViewById(R.id.textView1); // id of textview from layoutfile
就是这样,不要覆盖视图。
我用手机和平板电脑试过这个,我看不到它们。我有可能错过更基本的东西吗?我可以在没有做额外工作的情况下在MainActivity上看到一个按钮,我是否应该在另一个活动中执行其他操作以查看按钮? – 2013-04-24 00:08:53
@MertToka像其他人一样评论说它正在工作。尝试查看您的活动是否包含正确的layout.xml文件。 – Sharj 2013-04-24 00:17:35
你是对的,问题是因为setContentView线。但是如果不使用它,我怎么能在这个活动中看到来自MainActivity的textView?你能推荐我一些东西吗? – 2013-04-24 00:23:13
你以前的参考宣布ID是不正确的。
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1" <---no plus sign; "@id/textView1"
android:layout_marginLeft="74dp"
android:layout_marginTop="64dp"
android:layout_toRightOf="@+id/textView1" <---no plus sign;
android:text="@string/bttn_aux1"
android:onClick="is_clicked" />
以下字段有同样的问题。
仅当您第一次分配ID时才使用加号,以便它将声明该ID。当id已经被声明时,不需要使用加号。
感谢您的提示,很高兴知道这样的东西。我使用Graphical Layout创建了界面,所以我认为Eclipse弄糟了一些东西。但我仍然有这个问题,可能是因为别的吗? – 2013-04-23 23:39:34
@MertToka我也没有问题显示按钮了。也许你只是使用屏幕太小的仿真器设备。尝试在第一个按钮上删除'android:layout_marginLeft',看看是否有任何东西出现。 – StoneBird 2013-04-23 23:57:01
什么是您的设备的屏幕尺寸?或者,也许问题不在布局中,它在我的.java文件中。我应该做一些特定的活动文件中的按钮显示? – 2013-04-24 00:02:44
刚刚尝试过你的XML,我看到任何问题! – 2013-04-23 23:33:25
真的吗?我仍然遇到同样的问题。你认为这是由于我的虚拟设备设置或什么?我是新来的;所以我很抱歉我的新手问题。 – 2013-04-23 23:38:15
@MertToka我用正确的问题更新了答案。 – Sharj 2013-04-24 00:21:30