Android: 在另一个之上拉伸布局
Android: stretch layout above another
我使用 wrap_content
创建布局 (ContentLayout
)。
我想显示加载布局 (LoadingLayout
) 与上面的 ContentLayout
大小相同。
像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout.
android:id="@+id/ContentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
<RelativeLayout
android:id="@+id/LoadingLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
...
</RelativeLayout>
</RelativeLayout>
但是当我想在 onMeasure
中设置 LoadingLayout
大小时,它只会在第二次机会时设置为正确的大小。
只能像这样工作:
Activity
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) loadingLayout.getLayoutParams();
params.height = getMeasuredHeight();
loadingLayout.setLayoutParams(params);
super.onMeasure(widthMeasureSpec, heightMeasureSpec); // android realignment hack
}
我认为这是一个 hack。你有更好的解决方案吗?
如果我没理解错的话,你需要 LoadingLayout
来保持 ContentLayout
的宽度
<RelativeLayout
android:id="@+id/LoadingLayout"
android:layout_alignLeft="@id/ContentLayout"
android:layout_alignRight="@id/ContentLayout"
....>
....
</RelativeLayout>
编辑:下面是高度的一种方法
您可以在全局刷新发生时添加一次性侦听器。然后所有尺寸都可用。
// in your activity
@Override
protected void onCreate(Bundle state) {
final View loading = findViewById(R.id.LoadingLayout);
final View content = findViewById(R.id.ContentLayout);
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
loading.getLayoutParams().height = content.getHeight();
loading.onRequestLayout();
loading.invalidate();
}
}
}
我使用 wrap_content
创建布局 (ContentLayout
)。
我想显示加载布局 (LoadingLayout
) 与上面的 ContentLayout
大小相同。
像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout.
android:id="@+id/ContentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
<RelativeLayout
android:id="@+id/LoadingLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
...
</RelativeLayout>
</RelativeLayout>
但是当我想在 onMeasure
中设置 LoadingLayout
大小时,它只会在第二次机会时设置为正确的大小。
只能像这样工作:
Activity
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) loadingLayout.getLayoutParams();
params.height = getMeasuredHeight();
loadingLayout.setLayoutParams(params);
super.onMeasure(widthMeasureSpec, heightMeasureSpec); // android realignment hack
}
我认为这是一个 hack。你有更好的解决方案吗?
如果我没理解错的话,你需要 LoadingLayout
来保持 ContentLayout
<RelativeLayout
android:id="@+id/LoadingLayout"
android:layout_alignLeft="@id/ContentLayout"
android:layout_alignRight="@id/ContentLayout"
....>
....
</RelativeLayout>
编辑:下面是高度的一种方法
您可以在全局刷新发生时添加一次性侦听器。然后所有尺寸都可用。
// in your activity
@Override
protected void onCreate(Bundle state) {
final View loading = findViewById(R.id.LoadingLayout);
final View content = findViewById(R.id.ContentLayout);
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
loading.getLayoutParams().height = content.getHeight();
loading.onRequestLayout();
loading.invalidate();
}
}
}