【问题标题】:Button Style with slim border带细边框的按钮样式
【发布时间】:2015-07-29 13:41:37
【问题描述】:

我有一个带有如下按钮的 DialogFragment:

如何将按钮的样式更改为如下所示?

我正在扩展 DialogFragment,但想使用 AlertDialog.Builder 用于 defaultPositive/negative 按钮的样式。

【问题讨论】:

  • 更改主题或为您的对话框提供自定义布局。
  • 同时隐藏默认标题栏并制作自己的黄色标题栏。
  • 我要使用的样式是AlertDialog.Builder使用的样式,但是找不到样式的名称。
  • <style name="AppBaseTheme" parent="android:Theme.Holo"></style> 设置为您的Activity
  • 该活动使用不同的样式。不设置整个主题就不能设置按钮的样式吗?

标签: android button layout dialog


【解决方案1】:

试试这个

创建一个xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@drawable/header_logo"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:scaleType="center"
        android:background="#FFFFBB33"
        android:contentDescription="@string/app_name" />
    <EditText
        android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/username" />
    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="@string/password"/>
</LinearLayout>

然后在onCreate()

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();
}

然后在清单中

<activity android:theme="@android:style/Theme.Holo.Dialog" >

【讨论】:

  • 我没有使用 AlertDialog.Builder。我正在扩展 DialogFragment。
【解决方案2】:

是android 2.3以下的默认界面。你需要外部库让它的界面变成android 4.0以上的风格。您可以使用此library 来执行此操作。

【讨论】:

  • 不,这不是 2.3 的默认按钮样式。
  • 清单文件中的 targetSdkVersion 是什么?运行该对话框片段(第一张图片)时,您的 Android 版本是什么?
  • Min 和 target 都是 17。
  • 此应用的版本始终为 17。我只希望它看起来像第二种风格。我还需要 AppCompat 吗?我只对第一张图片中没有灰色背景的按钮感兴趣。
  • 编辑:你的安卓设备/模拟器版本?在第一张图片中,它是 Android 2.3 风格。而对于第二张图片,它是 Holo 风格。您需要使用 ActionbarSherlock 或 HoloEverywhere 来实现 Holo 样式。这是应用程序兼容性问题。
猜你喜欢
  • 2017-10-21
  • 2018-05-08
  • 2023-04-03
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多