我知道有相關的問題,但請嘗試我的代碼1的LayoutParams使用的addRules
我覺得我的代碼是正確的,但我不知道爲什麼我不能要我要
我嘗試將圖像替換爲具有相同LayoutParams尺寸和位置的RelativeLayout(以編程方式),然後在該RelativeLayout內部將WebView放在它的中心。 幾乎已經完成了,但我在Webview的佈局方面遇到了麻煩。 見下文
activity_main.java圖像
@Override
protected void onCreate(Bundle savedInstanceState) {
.........
// Image
ImageView image = (ImageView)findViewById(R.id.image);
// Container of the image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl_imageView);
loader(rl , image);
}
public void loader(RelativeLayout rl,View view){
// Creating RelativeLayout
RelativeLayout rlnew = new RelativeLayout(this);
// Getting the Layout Parameters of the image such as (position and dimension)
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
// Coloring the background of the new Relative Layout into blue
rlnew.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
// passing the parameters to the new relative layout from the image
rlnew.setLayoutParams(lp1);
// image turns into invisible
view.setVisibility(View.INVISIBLE);
// Creating a webView
WebView webView = new WebView(this);
// load the loading.gif
webView.loadDataWithBaseURL("file:///android_asset/", "<center><img src='loading.gif' style=''/></center>", "text/html", "utf-8", null);
// setting up dimension(height and weight) for the webview
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 60);
// adding position(Center) for the webview
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// setting up Layout parameters for the webiview
webView.setLayoutParams(lp);
// adding the webview to the new Relative Layout
rlnew.addView(webView);
// adding the new relative layout into the container
rl.addView(rlnew);
}
activity_main XML
<RelativeLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#ec0c0c"
android:id="@+id/rl_imageView"
android:layout_marginTop="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<!--Desired Output-->
<!--<RelativeLayout-->
<!--android:layout_width="70dp"-->
<!--android:layout_height="70dp"-->
<!--android:background="#010e6e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true">-->
<!--<WebView-->
<!--android:layout_width="60dp"-->
<!--android:layout_height="30dp"-->
<!--android:id="@+id/imageView"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true" />-->
<!--</RelativeLayout>-->
<!--Default Image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sample1"
android:id="@+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
</RelativeLayout>
我做的方法,這將要求兩個參數的RelativeLayout(即包含方法的第二個參數)和視圖,所以它可以是任何像(圖像,TextVie w,RelativeLayout等)。
該代碼可以替換特定視圖像的RelativeLayout或TextView的或圖像 並與給定的視圖相同的大小和位置的RelativeLayout替換它(我的是圖像)(如果你移動的時候,已經完成該圖像位於給定容器中的任何位置,代碼將將該圖像替換爲一個relativeLayout,而不管它在給定容器內的哪個位置)。
唯一的問題是WebView的位置。 。爲什麼webView在那裏? 。我希望在新的RelativeLayout
誰能告訴我才哪裏出了錯 如果你有另一種方式的中心僅有提交答案
嘗試刪除'yourView.setLayoutParams'直接添加它們,就像'rlnew.addView(yourView,params);' – NamNH
@John您是否看到圖像?相同的輸出。它沒有得到我想要的。 –
這就是一場完全的災難,重複使用LayoutParams是一個問題的來源,如果你創建一個乾淨的佈局而不是使用xml並且像這樣弄亂它,並且聽John的評論,那就好多了。 – Nanoc