Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中创建无边框按钮_Android_Layout_Button_Borderless - Fatal编程技术网

如何在Android中创建无边框按钮

如何在Android中创建无边框按钮,android,layout,button,borderless,Android,Layout,Button,Borderless,他们说可以使用无边框按钮(见下图),但没有真正解释如何使用。几周前有人在这里问了同样的问题:有一个答案被标记为“答案”,但我仍然迷路了,我看不出有什么办法可以对一个已经“关闭”的问题添加评论 回答者说 “查看主题属性按钮样式, 按钮按钮样式,和无边框按钮样式“ 但我仍然不知道如何实际使用这些东西。我在谷歌上搜索了一下,什么也找不到,所以我想我应该再问一次这个问题,希望有人能提供更多关于这个问题的细节 android:background="@android:color/transparent"

他们说可以使用无边框按钮(见下图),但没有真正解释如何使用。几周前有人在这里问了同样的问题:有一个答案被标记为“答案”,但我仍然迷路了,我看不出有什么办法可以对一个已经“关闭”的问题添加评论

回答者说

“查看主题属性
按钮样式
按钮按钮样式
,和
无边框按钮样式

但我仍然不知道如何实际使用这些东西。我在谷歌上搜索了一下,什么也找不到,所以我想我应该再问一次这个问题,希望有人能提供更多关于这个问题的细节

android:background="@android:color/transparent"

您还应该将图片的边距和填充设置为0。 另请查看第二个未标记的答案


几周前,当我在这里查看并注意到关于使用透明背景的答案时,我认为我已经解决了这个问题,但这还不够好,因为它可以防止按钮在按下时突出显示

此外,将样式设置为
Widget.Holo.Button.Borderless
也不合适,因为它会使按钮边界变大

为了一劳永逸地解决这个问题,我检查了标准日历应用程序的android源代码,发现它使用了以下内容:

android:background="?android:attr/selectableItemBackground"
这样做可以确保按钮无边界且大小正确

看看这个:

按钮
图像按钮
标签上的属性:

    style="?android:attr/borderlessButtonStyle"

几天前,一位律师又在这件事上犯了错误

以下是我的解决方案:

分两步完成:将按钮背景属性设置为android:attr/selectableItemBackground创建一个带有反馈但没有背景的按钮

android:background="?android:attr/selectableItemBackground"
将无边框按钮与布局的其余部分分开的行由背景为android:attr/dividervertial的视图完成

android:background="?android:attr/dividerVertical"
为了更好地理解,这里有一个在屏幕底部的OK/Cancel无边框按钮组合布局(如上图所示)


如果您使用ActionbarSherlock

<Button 
  android:id="@+id/my_button" 
  style="@style/Widget.Sherlock.ActionButton" />

此代码适用于我:

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerVertical" />

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:measureWithLargestChild="true"
    android:orientation="horizontal"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:paddingTop="0dip" >

    <Button
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickCancel"
        android:text="@string/cancel" />

     <Button
        android:id="@+id/info"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickInfo"
        android:visibility="gone"
        android:text="@string/info" />

    <Button
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickSave"
        android:text="@string/save" />
</LinearLayout>


我在底部显示了3个按钮

如果图像背景本身不是透明的,它不会有多大帮助。另外,android:background=“@null”工作这是正确的方法。我已经找了很长时间了。谢谢!如何从java代码而不是XML实现这一点?我正在代码中创建一个ImageButton,希望它无边框,但在触摸时也有高亮颜色SelectableItemBackground仅在API级别11支持,是否有针对旧版本的解决方案?@alexandroid您必须为较低版本创建自己的解决方案如果您使用HoloEverywhere
在minimumSdk 9中非常适合我会产生稍微超出视图范围的连锁反应的样式如何?例如,在棒棒糖的联系人应用程序中,当您搜索联系人,然后单击“向上”按钮(箭头)时?好像按钮本身是圆形的,但背景是透明的…@Dalc,太棒了working@Dalc如何以编程方式应用它?这适用于按钮,但当您对ImageButtons执行此操作时,虽然边框消失,但它无法仅将其宽度包裹到提供的图像,并开始像一个最小宽度的普通按钮一样工作。因此,对于ImageButtons,最好的方法是
android:background=“?android:attr/selectableItemBackground”
<Button 
  android:id="@+id/my_button" 
  style="@style/Widget.Sherlock.ActionButton" />
<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerVertical" />

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:measureWithLargestChild="true"
    android:orientation="horizontal"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:paddingTop="0dip" >

    <Button
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickCancel"
        android:text="@string/cancel" />

     <Button
        android:id="@+id/info"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickInfo"
        android:visibility="gone"
        android:text="@string/info" />

    <Button
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickSave"
        android:text="@string/save" />
</LinearLayout>