我试图将图像用作按钮。图像是椭圆形/圆形,因此当我将其设置为背景时,基本上是一个黑盒子,图像位于其上方。我该如何解决这个问题?
这是片段main xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
tools:context="com.zarwanhashem.thatwashard.MainActivity$PlaceholderFragment" >
<ImageButton
android:id="@+id/hardButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/hard_button_clickable"
android:background="@null"
android:onClick="hardButton"/>
这是硬按钮xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/hard_button_img" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/hard_button_img" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/hard_button_img"/>
</selector>
以下是应用中的问题: http://i.gyazo.com/a7f3b25341ebf4146d294df1f5660e99.png
我尝试将背景设置为null并将src设置为我的图像文件。什么都没有改变。
感谢。
电子;这是实际的PNG:http://tinypic.com/r/av3cli/8
电子;仍在寻求帮助!
电子;凹凸。没人知道发生了什么事吗?
MainActivity:
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void hardButton (View view) {
final MediaPlayer mp = MediaPlayer.create(this, R.raw.thatwashard);
mp.start();
}
}
电子;凹凸。仍然无法弄清楚这一点。查看第一个答案中的提交,以查看实际情况。
电子;凹凸。 @Lazy Ninja
电子;凸块
答案 0 :(得分:0)
我在GIMP中检查了你的形象。背景不透明。它是纯白色的。
更正您的图像文件,然后在ImageButton中使用以下内容。
android:src="@drawable/hard_button_clickable"
android:background="@android:color/transparent"
附加
我看过你修改后的实际图像。这是一个jpg 如果你想利用透明度,你应该使用png 一般来说,png是android的首选格式。
另外几点。 android:text
不支持android:textColor
,android:textSize
,ImageButton
。
答案 1 :(得分:0)
设置视图scr="@drawable/hard_button_clickable"
和android:background="@null"
可以解决您的问题
并且你不能在ImageButton中使用文本。
<ImageButton
android:id="@+id/hardButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/hard_button_clickable"
android:background="@null"
android:onClick="hardButton"/>