如何在 android 中的按钮内添加 ImageButton
how to add an ImageButton inside a button in android
我想在另一个按钮中制作一个按钮,
例如 :
我们有 2 个按钮:1.imageButton (100 * 50)dp,2.button (100 * 100)dp
所以我的问题是如何将 imageButton 放入我的按钮中?
您可以使用 RelativeLayout
并将第二个 ImageButton
放在第一个 ImageButton
上。
更新
或者您可以在LinearLayour
中使用magrin
,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true" />
<ImageButton
android:layout_marginLeft="-100dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true" />
</LinearLayout>
希望这对你有帮助..
您应该为此使用框架布局。
在 XML 文件中,这样做
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageButton
android:id="@+id/imagebutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="@drawable/buttonsample"/>
</FrameLayout>
现在在 java 文件中,声明 Button 和 ImageButton 的实例
ImageButton 图片按钮;
按钮按钮;
在 java class 的 onCreate() 函数中,执行....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagebutton = (ImageButton)findViewById(R.id.imagebutton);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff
}
});
imagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff
}
});
}
如果您希望按钮重叠并且两者都需要可见,请设置 z 高度 属性。 android:海拔=“2dp”
我想在另一个按钮中制作一个按钮, 例如 : 我们有 2 个按钮:1.imageButton (100 * 50)dp,2.button (100 * 100)dp 所以我的问题是如何将 imageButton 放入我的按钮中?
您可以使用 RelativeLayout
并将第二个 ImageButton
放在第一个 ImageButton
上。
更新
或者您可以在LinearLayour
中使用magrin
,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true" />
<ImageButton
android:layout_marginLeft="-100dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true" />
</LinearLayout>
希望这对你有帮助.. 您应该为此使用框架布局。 在 XML 文件中,这样做
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageButton
android:id="@+id/imagebutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="@drawable/buttonsample"/>
</FrameLayout>
现在在 java 文件中,声明 Button 和 ImageButton 的实例
ImageButton 图片按钮;
按钮按钮;
在 java class 的 onCreate() 函数中,执行....
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
imagebutton = (ImageButton)findViewById(R.id.imagebutton);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff
}
});
imagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff
}
});
}
如果您希望按钮重叠并且两者都需要可见,请设置 z 高度 属性。 android:海拔=“2dp”