2013-04-25 75 views
3

iam是Android.Now中的初学者级别的程序员,在一个小应用程序开发后,我有一个dialogFragment.Everything是完美的工作,它的显示对话框also.But我有一些颜色方案的困难。我已经改变了布局的背景颜色,但其标题栏颜色保持相同的白色,并且标题文本颜色为蓝色,并在其下面有一条蓝线(需要将其更改为绿色)。如何实现此目的? 请帮我定制android DialogFragment

这里是我的片段代码

public class ClientInfofrag extends DialogFragment { 

public ClientInfofrag() 
{ 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.clientinfolayout, container); 
    getDialog().setTitle("Client Info"); 

    return view; 
} 
} 

谢谢

+1

did you refer [developer.android.com](http://developer.android.com/reference/android/app/DialogFragment.html)他们给出了一个很容易学习的例子 – Gunaseelan 2013-04-25 13:58:53

回答

2

由于您使用的是.setTitle()方法是只设置与defualt设置,标题如白色背景。如果你想自定义标题背景颜色,你需要有一个XML来做到这一点。另外,对于DialogFragments,根据我的知识和经验,您应该使用public Dialog onCreateDialog而不是public View onCreateView。这样,你返回一个对话框,而不仅仅是一个视图,你可以打电话.show(),它会显示你的对话框。下面是一个例子:

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

    Bundle args = getArguments(); 
    currentName = args.getString(ARG_CURRENT_NAME); 

    builder.setView(inflater.inflate(R.layout.name_dialog, null)); 
    builder.setTitle("Rename Rapper Program"); 
    builder.setMessage("Enter a new name for " + currentName + ":"); 
    builder.setPositiveButton("Rename", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      newName = (EditText) getDialog().findViewById(R.id.new_name); 
      newProgName = newName.getText().toString(); 
      mRename.renameProgram(currentName, newProgName); 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      dismiss(); 
     } 
    }); 

    return builder.create(); 
} 

下面是一个例子对话框XML,虽然它不是在上述DialogFragment进行充气的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView android:drawableLeft="@drawable/login" 
     android:layout_width="match_parent" 
     android:layout_height="64dp" 
     android:background="#FCD116" 
     android:text="@string/login" 
     android:textSize="36sp"/> 
    <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/un"/> 
    <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/pw"/> 

</LinearLayout> 

LinearLayout正在建立的其余部分子项目相应放置。第一个TextView充当我的“标题”栏,然后EditText是对话框的“主体”。我在xml中没有按钮,因为我在上面的代码片段中以编程方式将它们设置在onCreateDialog之内。

// Remove the title 
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

试试:

+0

嗨,谢谢。 ..你能告诉我我们如何使xml – sam 2013-04-25 14:05:53

+0

@sam我编辑了我的答案。这可能有帮助。 – TronicZomB 2013-04-25 14:12:25

+0

它似乎工作。我仍然有默认背景的对话框的名称。 – jiahao 2013-08-12 11:21:22

0

以上(TronicZomB)的例子,如果你禁用默认的窗口标题可以工作!

相关问题