问题
iam a beginner level programmer in Android.Now iam after a small app development and i have a dialogFragment.Everything is perfectly working and its displaying Dialog box also.But i have some difficulties with color scheme. I have changed the background color of layout and but its title bar color remains same white and also title text color blue and a blue line under that(need to change it to green).How i can achieve this? please help me
here is my fragment code
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;
}
}
Thank you
回答1:
Since you are using the .setTitle()
method it is only setting the title with the defualt settings, such as the white background. If you want to customize the title background color you will need to have an xml to do that. Also, for DialogFragments, from my knowledge and experience, you should use public Dialog onCreateDialog
instead of public View onCreateView
. That way you return a Dialog as opposed to just a View that you can then just call .show()
on and it will display your dialog. Here is an example:
@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();
}
Here is an example dialog xml, though it is not the xml that is being inflated in the above DialogFragment:
<?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>
The LinearLayout
is setting up the rest of the child items to be placed accordingly. The first TextView
acts as my "title" bar and then the EditText
s are the "body" of the dialog. I have no buttons in the xml because I set those programmatically within the onCreateDialog
like in the other snippet of code above.
回答2:
The example of the above (TronicZomB) could work if you disable the default windows title:
// Remove the title
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Try it!
来源:https://stackoverflow.com/questions/16216615/customizing-android-dialogfragment