2011-04-01 109 views
0

如果我從代碼創建佈局如何解決解決方案的問題?解決問題與佈局

我在320 x 480分辨率的手機上測試過它,看起來沒問題。但是當我在我的朋友手機(Galaxy S)測試它時,看起來佈局沒有伸展。

爲什麼我從代碼創建佈局是因爲我不知道如何將按鈕放置在特定位置。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    rLayout = new RelativeLayout(this);//(RelativeLayout)findViewById(R.id.relativeLayout1); 
    rLayout.setBackgroundResource(R.drawable.menu_blankscreen); 
    InitializeLayout(); 
    setContentView(rLayout); 

} 


private void InitializeLayout() { 
    ImageButton newGame = new ImageButton(this); 
    //newGame.setBackgroundResource(drawable.menu_button_new); 
    newGame.setBackgroundResource(R.drawable.menu_button_new_xml); 
    newGame.setOnClickListener(newGameClick); 
    addView(newGame, 5, 174, 149, 41); 

    ImageView continueGame = new ImageView(this); 
    continueGame.setBackgroundResource(R.drawable.menu_button_continue_xml); 
    continueGame.setOnClickListener(continueClick); 
    addView(continueGame, 5, 218, 149, 41); 

    ImageView highscore = new ImageView(this); 
    highscore.setBackgroundResource(R.drawable.menu_button_highscore_xml); 
    highscore.setOnClickListener(highscoreClick); 
    addView(highscore, 5, 262, 149, 41); 

    ImageView achievement = new ImageView(this); 
    achievement.setBackgroundResource(R.drawable.menu_button_achievement_xml); 
    achievement.setOnClickListener(achievementClick); 
    addView(achievement, 5, 306, 149, 41); 

    ImageView option = new ImageView(this); 
    option.setBackgroundResource(R.drawable.menu_button_option_xml); 
    option.setOnClickListener(optionClick); 
    addView(option, 5, 350, 149, 41); 

    ImageView quit = new ImageView(this); 
    quit.setBackgroundResource(R.drawable.menu_button_quit_xml); 
    quit.setOnClickListener(quitClick); 
    addView(quit, 5, 395, 149, 41); 

    Button btnTest = new Button(this); 
    btnTest.setText("Test"); 
    btnTest.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(StartMenuActivity.this, TestActivity.class); 
      startActivity(intent); 
     } 
    }); 
    addView(btnTest, 270, 430,50, 50); 
} 

public void addView(View view, int x, int y, int width, int height) { 
    rParam = new RelativeLayout.LayoutParams(width, height); 
    rParam.leftMargin = x; 
    rParam.topMargin = y; 
    rLayout.addView(view, rParam); 
} 

我假設leftMargin爲x,topMargin爲y。 它是否與像素有關?

回答

1

您試圖將UI組件「絕對地」放置。 AbsoluteLayout已折舊,您的問題恰恰是原因。您可以爲您的dev設備實現UI組件的「完美」放置。當它在另一個具有不同像素密度或分辨率的設備上運行或...列表繼續時,您將遇到問題。

要實現完美,每個設備上的「絕對」放置既不切實際也不可能。您可以使用各種佈局和迭代屬性中的不同值,在大多數設備上真正實現像素完美貼圖。閱讀here瞭解更多信息。

這需要一些努力來挖掘範例,但它實際上相對於鞦韆來說非常簡單,至少對我而言是這樣。

這就是我對肖像視圖的看法:我們需要一個LL從上到下的流動,每個放置在內部的組件都會在上面或下面。然後我們需要另一個從左到右流動的LL(嵌套)。每個組件都將放置在每個組件的側面。然後,根據要將組件放置在與像素屏幕密度和分辨率大小「相對」的位置,然後指定重力,邊距,填充和重量。這裏有一個小例子來展示它的外觀。希望能幫助到你!

<LinearLayout 
     android:layout_width="match_parent" 
     android:id="@+id/linearLayout1" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:id="@+id/linearLayout2" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:gravity="center"> 
       <Button android:id="@+id/myButton" 
        style="@style/actionButton" 
        android:onClick="onClickFeature" 
        android:text="@string/title_button" 
        android:drawableTop="@drawable/pic_btn" 
        android:layout_marginBottom="5dip" 
        android:layout_marginTop="5dip"/> 
+0

我真的不明白..這是否意味着只是將按鈕放在我想要的位置將需要很大的努力? – Fugogugo 2011-04-01 05:21:28

+0

這意味着,雖然自定義視圖是可能的,甚至在某些情況下是可取的...如果你不知道你在做什麼,應該避免它們。需要努力去理解你想要做的事情。 – Martinez 2011-04-01 05:34:23