2013-08-19 41 views
1

我是新來的Android &這個優秀的問題/答案的網站..安卓:線性佈局之間的水平差距

目前我面臨的一個問題,內用線性佈局之間間隙/空間。

我有多個線性佈局嵌套(線性佈局 - >滾動視圖佈局 - >多個線性佈局)。

我的目標是顯示ImageView(與圖像)大小根據設備屏幕分辨率動態更改。

每個內部佈局都有ImageView控件。我寫了調整每個ImageView在運行時適合屏幕。在設計時,內部線性佈局顯示爲沒有橫向間隙,但是在朗姆酒時間由於動態代碼,它們是線性佈局之間的差距。

Design display 
____________ ____________ 
|   | |   | 
|   | |   | 
| ICON - 1 | | ICON - 2 | 
|   | |   | 
____________ ____________ 
    NO horizontal space 
____________ ____________ 
|   | |   | 
|   | |   | 
| ICON - 3 | | ICON - 4 | 
|   | |   | 
____________ ____________ 


Runtime display 
____________ ____________ 
|   | |   | 
|   | |   | 
| ICON - 1 | | ICON - 2 | 
|   | |   | 
____________ ____________ 
    un wanted horizontal space 
____________ ____________ 
|   | |   | 
|   | |   | 
| ICON - 3 | | ICON - 4 | 
|   | |   | 
____________ ____________ 

的main.xml代碼

<LinearLayout 
    android:background="@drawable/llrightborder" 
    android:id="@+id/mainlayoutLeft" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:orientation="vertical" > 

    <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <LinearLayout android:orientation="vertical" android:paddingTop="10.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="2dip">    

      <LinearLayout 
       android:id="@+id/leftRow1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" > 

       <ImageView 
        android:id="@+id/imageicon1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon1" 
        android:layout_marginRight="2dip" 
        android:clickable="true" /> 

       <ImageView 
        android:id="@+id/imageicon2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon2" 
        android:layout_marginLeft="2dip" 
        android:clickable="true" /> 

      </LinearLayout> 


      <LinearLayout android:id="@+id/leftRow2" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
       <ImageView 
        android:id="@+id/imageicon3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon3" 
        android:layout_marginRight="2dip" 
        android:clickable="true" /> 

       <ImageView 
        android:id="@+id/imageicon4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon4" 
        android:layout_marginLeft="2dip" 
        android:clickable="true" /> 
      </LinearLayout>      


     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

main.java用於顯示圖像尺寸代碼動態

DisplayMetrics metrics = getResources().getDisplayMetrics(); 

    iHeight = metrics.heightPixels; 
    iWidth = metrics.widthPixels; 

    leftLLWidth = (int) ((iWidth * 25)/100); 
    rightLLWidth = (int) (iWidth - leftLLWidth); 


    leftLL = (LinearLayout) findViewById(R.id.mainlayoutLeft); 
    rightLL = (LinearLayout) findViewById(R.id.mainlayoutRight); 


    leftLL.setLayoutParams(new LinearLayout.LayoutParams(leftLLWidth, iHeight)); 
    rightLL.setLayoutParams(new LinearLayout.LayoutParams(rightLLWidth, iHeight)); 

    //init image buttons 
    img_icon1 = (ImageView) findViewById(R.id.imageicon1); 
    img_icon2 = (ImageView) findViewById(R.id.imageicon2); 
    img_icon3 = (ImageView) findViewById(R.id.imageicon3); 
    img_icon4 = (ImageView) findViewById(R.id.imageicon4); 

    btnWidth = (int) ((leftLLWidth/2) - 3); 
    btnHeight = btnWidth + 60; 


    ViewGroup.LayoutParams button_params = img_icon1.getLayoutParams(); 

    button_params.width = btnWidth; 
    button_params.height = btnHeight; 

    img_icon1.setLayoutParams(button_params); 
    //img_icon1.setOnClickListener(onMainImgClickHandler); 


    img_icon2.setLayoutParams(button_params); 
    img_icon3.setLayoutParams(button_params); 
    img_icon4.setLayoutParams(button_params); 

希望這explaines我的問題。提前致謝。

問候 Nayeem

+0

你想顯示整個屏幕上4個圖標? –

回答

0

你試圖改變重力頂部?

android:layout_gravity="top" 
+0

是的,我試過android:layout_gravity =「top」但仍然沒有用... – nayeem

+0

在你的代碼中試試這個: 'LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(leftLLWidth,iHeight); LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(rightLLWidth,iHeight); params.gravity = Gravity.TOP; params.topMargin = 0; params2.gravity = Gravity.TOP; params2.topMargin = 0; leftLL.setLayoutParams(params); rightLL.setLayoutParams(params2);' – Dyna

+0

感謝Dyna,我試過上面提到的代碼... insted的減少空間它增加了空間...... :( – nayeem

0

在你父母的線性佈局,你已經把寬度

android:layout_width="0dip" 

這是不正確的

+0

是的,因爲佈局顯示動態,所以最初我固定爲0dip寬度和代碼我是變化的。 – nayeem