【发布时间】:2013-05-28 04:32:51
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:minWidth="@dimen/dialog_min_width"
android:padding="@dimen/dialog_padding"
android:background="@drawable/dialog_background" >
<TextView android:id="@+id/base_dialog_title"
style="@style/DialogTitleTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="16dp" >
</FrameLayout>
<!-- *********************** HERE ************************* -->
<FrameLayout android:id="@+id/base_dialog_content"
android:background="@drawable/dialog_description_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<!-- *********************** HERE ************************* -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="16dp" >
</FrameLayout>
<LinearLayout android:id="@+id/base_dialog_button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/base_dialog_button_negative"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
<Button
android:id="@+id/base_dialog_button_neutral"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
<Button
android:id="@+id/base_dialog_button_positive"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
这是对话框片段布局。
我将内容视图(TextView 或 ListView)添加到 FrameLayout (@id/base_dialog_content)
当 ListView 有很多 Items 时,Dialog 在窗口中是全高的。
我想设置最大高度对话框或内容视图(或列表视图)
编辑:
我使用 OnLayoutChangeListener 解决了这个问题。
但我在低版本(蜂窝以下)中需要相同的功能
在DialogFragment.onCreateView()中
FrameLayout contentContainer = (FrameLayout) view.findViewById(R.id.base_dialog_content);
final View contentView = createContentView(inflater, contentContainer);
contentContainer.addView(contentView);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// TODO Check content view height and change height
} else {
view.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
int height = v.getHeight();
int contentHeight = contentView.getHeight();
int winHeight = ActivityUtil.getDisplayHeight(getSherlockActivity());
LogUtils.pe(height+" / "+winHeight+" / "+contentHeight);
int needHeight = height - winHeight*8/10;
if (needHeight > 0) {
contentView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, contentHeight-needHeight));
}
}
});
}
【问题讨论】:
-
你想要的最大高度是多少?是在dp中定义的还是基于屏幕大小?
-
@NAYOSO“基于屏幕尺寸”更好。但是“dp 中的恒定值”对我也有好处。
-
请看我的回答:)
标签: android android-layout android-linearlayout