Android SDK 23:ToggleButton在旧Android版本中是透明的
我在我最新的Android SDK 23.1.1应用程序中使用了一些切换按钮。 我刚刚在三星Tab 1平板电脑上测试了安卓4.0.4和另一款安卓4.2.2平板电脑,可以看到我的切换按钮在这些版本中是半透明的 在现代索尼Xperia(5.1.1)平板电脑上的测试表明这些按钮是正确的 4.0.4和4.2.2单位:Android SDK 23:ToggleButton在旧Android版本中是透明的,android,togglebutton,Android,Togglebutton,我在我最新的Android SDK 23.1.1应用程序中使用了一些切换按钮。 我刚刚在三星Tab 1平板电脑上测试了安卓4.0.4和另一款安卓4.2.2平板电脑,可以看到我的切换按钮在这些版本中是半透明的 在现代索尼Xperia(5.1.1)平板电脑上的测试表明这些按钮是正确的 4.0.4和4.2.2单位: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<ToggleButton
android:id="@+id/button_plov"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Plov Fra"
android:textOn="Plov Til" />
<ToggleButton
android:id="@+id/button_salt"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Salt Fra"
android:textOn="Salt Til" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<Button
android:id="@+id/button_plov"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
<Button
android:id="@+id/button_salt"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
m_ButtonPlov.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean plov = !(m_Pref.getBoolean("PLOV", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("PLOV", plov);
m_Editor.apply();
updateUI();
}
});
m_ButtonSalt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean salt = !(m_Pref.getBoolean("SALT", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("SALT", salt);
m_Editor.apply();
updateUI();
}
});
private void updateUI() {
boolean plov = m_Pref.getBoolean("PLOV", false);
if (plov) {
m_ButtonPlov.setText("Plov til");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonPlov.setText("Plov fra");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
boolean salt = m_Pref.getBoolean("SALT", false);
if (salt) {
m_ButtonSalt.setText("Salt til");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonSalt.setText("Salt fra");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
}
5.1.1单位:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<ToggleButton
android:id="@+id/button_plov"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Plov Fra"
android:textOn="Plov Til" />
<ToggleButton
android:id="@+id/button_salt"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Salt Fra"
android:textOn="Salt Til" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<Button
android:id="@+id/button_plov"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
<Button
android:id="@+id/button_salt"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
m_ButtonPlov.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean plov = !(m_Pref.getBoolean("PLOV", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("PLOV", plov);
m_Editor.apply();
updateUI();
}
});
m_ButtonSalt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean salt = !(m_Pref.getBoolean("SALT", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("SALT", salt);
m_Editor.apply();
updateUI();
}
});
private void updateUI() {
boolean plov = m_Pref.getBoolean("PLOV", false);
if (plov) {
m_ButtonPlov.setText("Plov til");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonPlov.setText("Plov fra");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
boolean salt = m_Pref.getBoolean("SALT", false);
if (salt) {
m_ButtonSalt.setText("Salt til");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonSalt.setText("Salt fra");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<ToggleButton
android:id="@+id/button_plov"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Plov Fra"
android:textOn="Plov Til" />
<ToggleButton
android:id="@+id/button_salt"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Salt Fra"
android:textOn="Salt Til" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<Button
android:id="@+id/button_plov"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
<Button
android:id="@+id/button_salt"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
m_ButtonPlov.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean plov = !(m_Pref.getBoolean("PLOV", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("PLOV", plov);
m_Editor.apply();
updateUI();
}
});
m_ButtonSalt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean salt = !(m_Pref.getBoolean("SALT", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("SALT", salt);
m_Editor.apply();
updateUI();
}
});
private void updateUI() {
boolean plov = m_Pref.getBoolean("PLOV", false);
if (plov) {
m_ButtonPlov.setText("Plov til");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonPlov.setText("Plov fra");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
boolean salt = m_Pref.getBoolean("SALT", false);
if (salt) {
m_ButtonSalt.setText("Salt til");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonSalt.setText("Salt fra");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
}
有什么想法吗?
谢谢哇,这是安静的24小时。。。这个问题可能没有好的答案,但有一个解决办法 嗯,这是我的。我去掉了切换按钮,用普通按钮做了一个简单的实现 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<ToggleButton
android:id="@+id/button_plov"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Plov Fra"
android:textOn="Plov Til" />
<ToggleButton
android:id="@+id/button_salt"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Salt Fra"
android:textOn="Salt Til" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<Button
android:id="@+id/button_plov"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
<Button
android:id="@+id/button_salt"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
m_ButtonPlov.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean plov = !(m_Pref.getBoolean("PLOV", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("PLOV", plov);
m_Editor.apply();
updateUI();
}
});
m_ButtonSalt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean salt = !(m_Pref.getBoolean("SALT", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("SALT", salt);
m_Editor.apply();
updateUI();
}
});
private void updateUI() {
boolean plov = m_Pref.getBoolean("PLOV", false);
if (plov) {
m_ButtonPlov.setText("Plov til");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonPlov.setText("Plov fra");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
boolean salt = m_Pref.getBoolean("SALT", false);
if (salt) {
m_ButtonSalt.setText("Salt til");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonSalt.setText("Salt fra");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
}
结果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<ToggleButton
android:id="@+id/button_plov"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Plov Fra"
android:textOn="Plov Til" />
<ToggleButton
android:id="@+id/button_salt"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle"
android:textOff="Salt Fra"
android:textOn="Salt Til" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/text_rute_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Indlæser ruter"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/image_running"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="@drawable/run_stop" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_info"
android:background="@color/accent"
android:paddingTop="1dp">
<dk.gis34.openlayers.GIS34MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_plov_salt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layout_button"
android:orientation="vertical"
android:paddingLeft="5dp">
<Button
android:id="@+id/button_plov"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
<Button
android:id="@+id/button_salt"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/toggle_off"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<Button
android:id="@+id/button_logging"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="START"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="AFSLUT"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
m_ButtonPlov.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean plov = !(m_Pref.getBoolean("PLOV", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("PLOV", plov);
m_Editor.apply();
updateUI();
}
});
m_ButtonSalt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean salt = !(m_Pref.getBoolean("SALT", false));
m_Editor = m_Pref.edit();
m_Editor.putBoolean("SALT", salt);
m_Editor.apply();
updateUI();
}
});
private void updateUI() {
boolean plov = m_Pref.getBoolean("PLOV", false);
if (plov) {
m_ButtonPlov.setText("Plov til");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonPlov.setText("Plov fra");
m_ButtonPlov.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
boolean salt = m_Pref.getBoolean("SALT", false);
if (salt) {
m_ButtonSalt.setText("Salt til");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_on, 0);
} else {
m_ButtonSalt.setText("Salt fra");
m_ButtonSalt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toggle_off, 0);
}
}