我通过PreferenceFragment显示我的应用程序的首选项,这是一个DialogFragment。工作非常好99%。
如何将工具栏添加到特定的DialogFragment?
布局通过addPreferencesFromResource(R.xml.preferences)加载。
所以,我没有可以添加工具栏的布局。
编辑:下面我找到了一个很好的解决方案!!
答案 0 :(得分:0)
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/orange"
android:orientation="horizontal">
<ImageView
android:id="@+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/ic_arrow_back_black_36dp" />
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="@string/first_name"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="OK"
android:padding="10dp"
android:gravity="center"
android:background="@color/holo_green_light"
android:textColor="@android:color/white"
android:layout_weight="1"/>
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="CANCEL"
android:padding="10dp"
android:gravity="center"
android:background="@color/holo_red_light"
android:textColor="@android:color/white"
android:layout_weight="1"/>
</LinearLayout>
要使用cardview,它会为您提供阴影功能,就像工具栏一样,在您的应用的build.gradle中使用以下依赖项。
编译'com.android.support:cardview-v7:23.0。+'
public class CustomDialogFragment extends DialogFragment { public CustomDialogFragment(){ } @Override public Dialog onCreateDialog(Bundle onSavedInstanceState){ Dialog dialog = super.onCreateDialog(onSavedInstanceState); // Hide default title. dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); return dialog; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle onSavedInstanceState){ View v = inflater.inflate(R.layout.custom_dialog_layout, container, false); ImageView backButton = (ImageView) v.findViewById(R.id.back_button); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** TODO handle the Back button (OR) Communicate to the MainActivity to dismiss the dialog To know more about communicating from fragment to main activity check this thread http://stackoverflow.com/a/34498055/3049065 **/ } }); Button okButton = (Button) v.findViewById(R.id.ok); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO handle the ok Button } }); Button cancelButton = (Button) v.findViewById(R.id.cancel); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO handle the cancel Button } }); return v; } }
检查我的存储库下面的分支以获取演示项目。
https://github.com/Mahendran-Sakkarai/Testing-Android-Applications/tree/CustomDialogFragment