2016-11-14 84 views
0

我想在另一个按钮中创建一个按钮,例如: 我们有两个按钮:1.imageButton(100 * 50)dp,2.button(100 * 100) )dp 所以我的问题是我怎么能把我的imageButton在我的按钮内?如何在一个按钮中添加一个ImageButton android

+1

你能澄清一下吗?这没有任何意义,你的意思是添加一个图像按钮? – Pztar

+0

不,我可以使用android:drawableLeft或...在按钮内添加图像。我的问题是这样的。我想要按钮与图像,当我点击按钮做某事,当我点击按钮中的图像做其他事情 – b4hr4m

+1

您在布局中添加视图,而不是视图内的视图。您可以简单地在布局中添加图像,并为布局和图像按钮设置不同的点击事件。 –

回答

1

您可以使用RelativeLayout,并在第一个ImageButton之后放置第二个ImageButton

更新

或者您可以使用magrinLinearLayour,例如:

<?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> 
+0

在线性布局是不可能的? – b4hr4m

+0

基本上,如果你想使这种类型的功能使用相对布局,并添加相对布局,然后打开你的设计视图,并将该图像拖放到按钮 –

+0

@ b4hr4m是的,它可能在'LinearLayout'中。你应该使用'边距'来做到这一点(见更新的答案)。 –

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"> 

    <Button 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignParentTop="true" /> 

    <ImageButton 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 

这应该解决的麻烦。

0

希望这可以帮到你.. 你应该为此使用帧布局。 在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文件,宣布巴顿和ImageButton的

的ImageButton ImageButton的实例;

按钮按钮;

在的onCreate()java类的功能,这样做....

保护无效的onCreate(捆绑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 
    } 
}); 

}

0

组Z海拔财产,如果你想要的按钮重叠,两者必须都看得到。 android:elevation =“2dp”