在 android 中以编程方式创建布局 - 问题 [英] Create Layout Programmatically in android - issue
问题描述
我正在使用以下代码动态创建 FrameLayout
I am creating FrameLayout dynamically using the following code
mylayout.java
mylayout.java
FrameLayout layout = new FrameLayout(this);
FrameLayout.LayoutParams layoutparams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
layout.setLayoutParams(layoutparams);
webview=new WebView(this);
webview.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
splashview=new ImageView(this);
splashview.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
splashview.setScaleType(ScaleType.FIT_XY);
splashview.setImageResource(R.drawable.splash);
splashview.setVisibility(View.INVISIBLE);
progressbar=new ProgressBar(this);
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT,Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
progressbar.setLayoutParams(params);
progressbar.setVisibility(View.INVISIBLE);
layout.addView(webview);
layout.addView(progressbar);
layout.addView(splashview);
setContentView(layout);
mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="invisible" />
<ImageView
android:id="@+id/splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/myimg"
android:scaleType="fitXY"
android:src="@drawable/splash"
android:visibility="invisible" />
</FrameLayout>
我需要 java 中上述 xml 文件的等价代码
.我犯了什么错误?
I need the equalent code for the above xml file in java
. what mistake i did?
注意:使用上面的java代码,每一个都是重叠的.
Note: Using the above java code, each one is overlapping.
推荐答案
通常,FrameLayout 应该用于保存单个子视图,因为很难以可缩放到不同屏幕尺寸的方式组织子视图,而子视图又不会相互重叠.但是,您可以将多个子项添加到 FrameLayout 并通过使用 android:layout_gravity
属性为每个子项分配重力来控制它们在 FrameLayout 中的位置.
Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity
attribute.
在您的情况下,您可以使用 layout.setForegroundGravity(int)
相同.
In your case, you may use layout.setForegroundGravity(int)
for the same.
这篇关于在 android 中以编程方式创建布局 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!