我创建了一个稍微简单的布局,共有 4 个按钮:布局底部的 3 个按钮并排(在 RelativeLayout 中)和几乎在屏幕中间的巨大按钮上。
在冰淇淋三明治和软糖豆上,我可以单击任何按钮,一切正常(即调用各自的onClick函数),但奇怪的是,在姜饼上,底部的3个按钮中的任何一个都可以工作,但中间的按钮不起作用。 onClick 触发了许多指令,据我所知,这些指令都没有被执行, 它没有被召唤,我不知道为什么。
布局代码如下。任何帮助将不胜感激!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_off"
android:orientation="vertical"
android:weightSum="10" >
<ImageButton
android:id="@+id/button_activate"
android:layout_width="155dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="170dp"
android:layout_weight="7.2"
android:background="@drawable/transparent"
android:clickable="true"
android:contentDescription="@string/app_name"
android:onClick="activate"
android:src="@drawable/transparent" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginTop="80dp"
android:layout_weight="2" >
<ImageButton
android:id="@+id/button_help"
android:layout_width="90dp"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:background="@drawable/transparent"
android:clickable="true"
android:contentDescription="@string/app_name"
android:onClick="openhelp"
android:src="@drawable/transparent" />
<ImageButton
android:id="@+id/button_settings"
android:layout_width="55dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginTop="0dp"
android:layout_toLeftOf="@+id/button_help"
android:background="@drawable/transparent"
android:clickable="true"
android:contentDescription="@string/app_name"
android:onClick="opensettings"
android:src="@drawable/transparent" />
<ImageButton
android:id="@+id/button_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginTop="0dp"
android:layout_toRightOf="@+id/button_help"
android:background="@drawable/transparent"
android:clickable="true"
android:contentDescription="@string/app_name"
android:onClick="opencontact"
android:src="@drawable/transparent" />
</RelativeLayout>
</LinearLayout>
调用布局的活动(主.xml):
//bunch of imports
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById (R.id.main_layout);
boolean serviceOn = isMyServiceRunning();
if(serviceOn==true) layout.setBackgroundResource(R.drawable.main_on);
else if (serviceOn==false) layout.setBackgroundResource(R.drawable.main_off);
}
@Override
protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById (R.id.main_layout);
boolean serviceIsOn = isMyServiceRunning();
if(serviceIsOn==true) layout.setBackgroundResource(R.drawable.main_on);
else if (serviceIsOn==false) layout.setBackgroundResource(R.drawable.main_off);
}
public void activate(View v){
LinearLayout layout = (LinearLayout) findViewById (R.id.main_layout);
Resources res = getResources();
Drawable on = res.getDrawable(R.drawable.main_on);
Drawable off = res.getDrawable(R.drawable.main_off);
if (layout.getBackground().getConstantState() == off.getConstantState()){
layout.setBackgroundResource(R.drawable.main_on);
Toast.makeText(getApplicationContext(), "App has been activated", Toast.LENGTH_SHORT).show();
turnon();
}
else if (layout.getBackground().getConstantState() == on.getConstantState()){
layout.setBackgroundResource(R.drawable.main_off);
Toast.makeText(getApplicationContext(), "App has been deactivated", Toast.LENGTH_SHORT).show();
turnoff();
}
}
}
我可以在我的模拟器上运行 2.2 和 2.3 的良好状态。
我唯一能建议的是尝试删除可绘制对象(也许有些设置搞砸了),或者尝试使您的相对布局高度为 0dp,因为您使用的是weight_sum