2012-04-20 55 views
0

這個UI(Android的XML)我有這個main.xml如何創建Java代碼

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/ic_launcher"/> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="@android:color/white" 
     android:orientation="horizontal" > 
     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:background="@drawable/barslide" 
      android:layout_marginLeft="500dp"/> 
    </LinearLayout> 
</FrameLayout> 

我想創建同一UImain.xml在Java代碼中。我嘗試編碼,但它不起作用,它不同於xml。下面是代碼:

void createUI(){ 

    LayoutParams params1 = new FrameLayout 
    (LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 

    LayoutParams params2 = new android.widget.FrameLayout.LayoutParams(
    android.widget.FrameLayout.LayoutParams.FILL_PARENT, 
android.widget.FrameLayout.LayoutParams.FILL_PARENT, 
Gravity.BOTTOM); 

FrameLayout f1 = new FrameLayout(this); 
f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

LinearLayout l2 = new LinearLayout(this); 
l2.setOrientation(LinearLayout.HORIZONTAL); 
l2.setLayoutParams(params2); 

view1 = new page1(getBaseContext()); 
view360 = view1.img1(getBaseContext()); 
view360.setBackgroundDrawable(getResources().getDrawable(R.drawable.black1)); 
view360.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 

slidebar = view1.img3(getBaseContext()); 
slidebar.setBackgroundResource(R.drawable.barslide); 
slidebar.setLayoutParams(new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT,Gravity.BOTTOM)); 


f1.addView(view360); 
f1.addView(l2); 
l2.addView(slidebar); 

addContentView(f1, params1); 
} 
+2

添加您嘗試的代碼。 – Luksprog 2012-04-20 06:27:49

+0

我不能添加代碼: 糟糕!您的編輯無法提交,因爲: 您的帖子沒有太多的上下文來解釋代碼段;請更清楚地解釋你的情況。 – Zerntrino 2012-04-20 06:32:50

+0

爲什麼我不能添加代碼。 T T – Zerntrino 2012-04-20 06:47:35

回答

0

您的代碼並不完全複製xml佈局(這是大的註釋):

  // replace THIS(from the constructors) with your context reference 
    FrameLayout fl = new FrameLayout(this); 
    fl.setLayoutParams(new FrameLayout.LayoutParams(
      FrameLayout.LayoutParams.MATCH_PARENT, 
      FrameLayout.LayoutParams.MATCH_PARENT)); 
    ImageView imv1 = new ImageView(this); // the first ImageView 
    imv1.setLayoutParams(new FrameLayout.LayoutParams(
      FrameLayout.LayoutParams.MATCH_PARENT, 
      FrameLayout.LayoutParams.MATCH_PARENT)); 
    imv1.setImageResource(R.drawable.ic_launcher); 
    fl.addView(imv1); 
    LinearLayout ll = new LinearLayout(this); 
    FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(
      FrameLayout.LayoutParams.MATCH_PARENT, 
      FrameLayout.LayoutParams.WRAP_CONTENT); 
    llp.gravity = Gravity.BOTTOM; 
    ll.setLayoutParams(llp); 
    ll.setBackgroundColor(Color.WHITE); 
    ImageView imv2 = new ImageView(this); // the second ImageView 
    DisplayMetrics metrics = getResources().getDisplayMetrics(); 
    float width = 300f;  
    int pWidth = (int) (metrics.density * width + 0.5f); 
    LinearLayout.LayoutParams imvlp = new LinearLayout.LayoutParams(pWidth, 
      FrameLayout.LayoutParams.WRAP_CONTENT); 
    float margin = 500f; 
    int pMargin = (int) (metrics.density * margin + 0.5f); 
    imvlp.leftMargin = pMargin; 
    imv2.setLayoutParams(imvlp); 
    imv2.setBackgroundResource(R.drawable.food_image1); 
    ll.addView(imv2); 
    fl.addView(ll); 

你必須修改它使用您的對象page1(我不確切知道你在那裏做什麼)。 此外,你確定你必須使用addContentView()(也許setContentView?)的方法嗎?(這種方法將視圖添加到已有的佈局)

+0

非常感謝! 它與xml相同。^^ – Zerntrino 2012-04-20 07:30:13