[英]Set the padding/margin to the top activity/fragment layout container in theme?
[英]Layout margin/padding at the top of dialog fragment
使用片段時,我的布局會受到頂部額外空間的干擾,我不知道它來自何處。 它看起來像這樣:
這個空地的可能來源是什么? 我還沒有找到主題或一些樣式設置,或者這個空間是否留給了動作欄? 我真的想擺脫這個。
以下是相應的布局xml和實例化對話框的代碼:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search_progress"
style="?android:attr/progressBarStyle"
android:indeterminate="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/search_progress"
android:text="Searching for:" />
<TextView
android:id="@+id/searchingNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_alignBottom="@+id/search_progress"
android:layout_alignLeft="@+id/textView1"
android:text="Barcelona" />
</RelativeLayout>
Java的:
FragmentManager fm = this.getSupportFragmentManager();
mSearchingDialog = new SearchingDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("name", mCityToAdd.userName());
mSearchingDialog.setArguments(bundle);
mSearchingDialog.show(fm, TAG);
我也在這里問過這個問題,認為這是一個與滾動視圖相關的問題,但空間出現在我的所有片段中:
嘗試調用getWindow().requestFeature(Window.FEATURE_NO_TITLE);
就在 setContentView(R.layout.<dialog_layout>);
在對話框的onCreate()
或onCreateView()
方法中。
有兩種方法可以重用/刪除DialogFragment
頂部的空白區域。
如果你想簡單刪除它:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); // <--
View view = inflater.inflate(R.layout.fragment_dialog, container, false);
return view;
}
如果要將其重用於對話框標題:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle("Some title..."); // <--
View view = inflater.inflate(R.layout.fragment_dialog, container, false);
return view;
}
又一個解決方案:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, getActivity().getApplicationInfo().theme);
}
此示例使用應用程序默認主題。 另請注意,需要在onCreate()
調用setStyle()
,而不是onCreateView()
才能生效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.