我正在制作一个扩展LinearLayout
的课程。在那我试图以编程方式进行布局。以下是我到目前为止所做的事情:
RelativeLayout.LayoutParams parentParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(parentParam);
this.setOrientation(LinearLayout.VERTICAL);
WebView webView = new WebView(context);
LinearLayout.LayoutParams webViewParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
webView.setLayoutParams(webViewParam);
this.addView(webView);
pb = new ProgressBar(context);
LinearLayout.LayoutParams pbParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
pbParam.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
pb.setLayoutParams(pbParam);
webView.addView(pb);
我的所有内容都运行正常,WebView
和ProgressBar
可见并执行各自的任务,但我正在尝试将ProgressBar
放入CENTER
屏幕。
我在这里缺少什么。任何形式的投入都将受到赞赏。
修改 现在输出如下:
答案 0 :(得分:2)
// try this way
1. Custom Linear Layout
public class MyCustomLinearLayout extends LinearLayout {
private FrameLayout frameLayout;
private ProgressBar pb;
private WebView webView;
public MyCustomLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MyCustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyCustomLinearLayout(Context context) {
super(context);
init(context);
}
private void init(Context mContext) {
frameLayout = new FrameLayout(mContext);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
webView = new WebView(mContext);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress >= 1) {
pb.setVisibility(View.VISIBLE);
}
if (newProgress == 100) {
pb.setVisibility(View.GONE);
}
}
});
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
frameLayout.addView(webView);
pb = new ProgressBar(mContext);
FrameLayout.LayoutParams parms = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
parms.gravity = Gravity.CENTER;
frameLayout.addView(pb,parms);
addView(frameLayout);
}
}
答案 1 :(得分:1)
以下是我将ProgressBar
置于中心所做的工作。为了我的目的,我使用了ScrollView
。这是我使用的代码:
RelativeLayout.LayoutParams parentParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(parentParam);
this.setOrientation(LinearLayout.VERTICAL);
scrollView = new ScrollView(context);
scrollView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT,
Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL));
scrollView.setFillViewport(true);
scrollView.setBackgroundColor(Color.TRANSPARENT);
pb = new ProgressBar(context);
LinearLayout.LayoutParams pbParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
pb.setLayoutParams(pbParam);
RelativeLayout rel = new RelativeLayout(context);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relParam.addRule(RelativeLayout.CENTER_IN_PARENT);
rel.addView(pb, relParam);
scrollView.addView(rel);
webView = new WebView(context);
LinearLayout.LayoutParams webViewParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
webViewParam.gravity = Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL;
webView.setLayoutParams(webViewParam);
this.addView(scrollView);
this.addView(webView);
希望,这将有助于其他人,他们正在尝试同样的事情。
答案 2 :(得分:0)
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(150, 150);
lp.weight = 1.0f;
lp.gravity = Gravity.CENTER;
ProgressBar pDialog = new ProgressBar(currentActivity);
pDialog.setIndeterminateDrawable(currentActivity.getResources().getDrawable(R.drawable.progressload));
LinearLayout ll = new LinearLayout(currentActivity);
ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(pDialog, lp);