2012-05-10 39 views
0

我希望为我的示例应用程序创建一个自定义布局。以前我使用xml布局进行用户界面,但是现在我希望使用自定义(不使用xml布局)创建应用程序。我在我的xml中使用了下面的代码,但是我将这个xml布局更改为我无法实现的自定义代码(我不知道如何做到这一点)。我如何在我的Activity中编写这个布局代码?任何人都可以请你帮我解决这个问题。提前致谢。如何在Android Activity中编写此布局代码?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/webviewscroll" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
      android:id="@+id/webviewlinear" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <RelativeLayout 
       android:id="@+id/webviewframe1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 

       <WebView 
        android:id="@+id/webview1" 
        android:layout_width="fill_parent" 
        android:layout_height="350dp" 
        android:layout_alignParentBottom="true" 
        android:layout_marginBottom="10dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="20dp" > 
       </WebView> 

       <ImageView 
        android:id="@+id/webviewimage1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="@drawable/one" > 
       </ImageView> 
      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/webviewframe2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 

       <WebView 
        android:id="@+id/webview2" 
        android:layout_width="fill_parent" 
        android:layout_height="350dp" 
        android:layout_alignParentBottom="true" 
        android:layout_marginBottom="10dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="20dp" > 
       </WebView> 

       <ImageView 
        android:id="@+id/webviewimage2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/two" > 
       </ImageView> 
      </RelativeLayout> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

回答

0
LinearLayout ll1,ll2; 
    ScrollView sv = new ScrollView(this); 
    RelativeLayout rl1,rl2; 
    WebView wv1,wv2; 
    ImageView iv1,iv2; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     int LHeightWrap = LinearLayout.LayoutParams.WRAP_CONTENT; 
     int LWidthWrap = LinearLayout.LayoutParams.WRAP_CONTENT; 

     int LHeightFill = LinearLayout.LayoutParams.FILL_PARENT; 
     int LWidthFill = LinearLayout.LayoutParams.FILL_PARENT; 

     int RHeightWrap = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     int RWidthWrap = RelativeLayout.LayoutParams.WRAP_CONTENT; 

     int RHeightFill = RelativeLayout.LayoutParams.FILL_PARENT; 
     int RWidthFill = RelativeLayout.LayoutParams.FILL_PARENT; 



     wv1 = new WebView(this); 
     iv1 = new ImageView(this); 

     rl1.addView(wv1,RWidthWrap,RHeightWrap); 
     rl1.addView(iv1,RWidthWrap,RHeightWrap); 

     wv2 = new WebView(this); 
     iv2 = new ImageView(this); 

     rl2.addView(wv2,RWidthWrap,RHeightWrap); 
     rl2.addView(iv2,RWidthWrap,RHeightWrap); 

     ll2.addView(rl1); 
     ll2.addView(rl2); 

     sv.addView(ll2); 

     ll1.addView(ll2); 

你可以做这样的事情。您还可以使用本机代码设置任何组件的属性。

1

当你要创建在运行时的布局,你可以这样说:

RelativeLayout layout1 = new RelativeLayout(this); 
ImageView imgView1 = new imgView1(this); 
layout1.addView(imgView1); // added ImageView into the RelativeLayout 

做这样的过程的创建和添加小部件。

+1

尼斯答案+1 :) – Lucifer