我有 3 层,它们都是 RelativeLayouts 或者基于它:
内容类
覆盖类
信息框布局
在我的代码中,我生成了一个覆盖信息框布局的覆盖对象。然后我将覆盖层添加到内容层。 我的目标是将父级(=覆盖)中的信息框居中(垂直和水平),但它不起作用。它是顶部/左侧。我认为这是默认行为。 所以这是我的代码。 我像这样将覆盖层添加到内容层:
private void openContent (String url,final ContentLayer content) {
overlay = new Overlay(context, url);
content.addView(overlay);
}
这是覆盖层:
public class Overlay extends RelativeLayout {
// some stuff
public Overlay(Context context, String url) {
super(context);
this.context = context;
this.url = url;
init(context, null, -1);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
this.setTag(context.getResources().getString(R.string.overlay));
RelativeLayout.LayoutParams lp = new RelativeLayoutout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
this.setLayoutParams(lp);
this.setBackgroundColor(context.getResources().getColor(R.color.black));
this.getBackground().setAlpha(80);
isMax = getValueInPrefs();
inflateLayout();
}
private void inflateLayout () {
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(service);
inflater.inflate(R.layout.infobox_wrapper, this, true);
}
// some stuff
}
最后但同样重要的是,这是我的信息框布局,名为 infobox_wrapper.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/epaper_webview_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"> <!-- DOESN'T WORK!! -->
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/grau_actionbar">
<Button
android:id="@+id/header_button_close"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="X"
android:layout_alignParentRight="true"
android:layout_margin="5dp"/>
<Button
android:id="@+id/header_button_min_max"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="M"
android:layout_toLeftOf="@id/header_button_close"
android:layout_margin="5dp"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/webview_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grau_actionbar"
android:layout_below="@id/header">
<WebView
android:id="@+id/epaper_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</RelativeLayout>
我感谢任何帮助或提示。
最佳答案
我是这样解决问题的: 我在 RelativeLayout(@+id/epaper_webview_wrapper) 中删除了 android:layout_centerInParent="true"并在 openContent-method 中动态设置它:
private void openContent (String url,final ContentLayer content) {
overlay = new Overlay(context, url);
RelativeLayout epaper_webview_wrapper= (RelativeLayout)overlay.findViewById(R.id.epaper_webview_wrapper);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)epaper_webview_wrapper.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
epaper_webview_wrapper.setLayoutParams(lp);
content.addView(overlay);
}
关于android:layout_centerInParent/RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20303492/