2013-01-13 90 views
1

令人困惑的標題的道歉。我的問題在下面的圖片中得到了更好的解釋:enter image description here將按鈕與另一個佈局中的圖像對齊

我需要將綠色按鈕與圖像頂部對齊,但圖像位於另一個佈局內。這可能嗎?

如果需要,可以在代碼中完成; XML不是必需的。我的目標是Android 2.2和更新的。

編輯:

我當前的實現是簡單地設置按鈕的MarginTop屬性,但是這是不方便的,當我需要改變,我打算根據做的LinearLayout,裏面的文字大小屏幕尺寸。

我認爲可以通過某種方式找到圖像的Y座標,也許通過添加TextViews的高度,然後將其設置爲Button的MarginTop來解決,但這聽起來很麻煩。真的沒有其他選擇嗎?

LinearLayout將被放置在一個ViewPager中(具有多個視圖,所有視圖都在同一位置),這就是爲什麼我不能像preeya解釋的那樣去做。

回答

1

這是可能的,但不是包括按鈕到相同的佈局更加複雜。如果你肯定不想這樣做,你不能使用XML(它總是更快)。你必須做你的代碼3個步驟:

1)等到圖繪

private void waitForViewToBeDrawn(){ 
    // get your layout 
    final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout); 
    ViewTreeObserver vto = mainLayout.getViewTreeObserver(); 
    // add a listener 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     public void onGlobalLayout() { 
      // you also want to remove that listener 
      mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      // go on to next step 
      getPositionOfImageView(); 

     } 
    }); 
} 

這種方法最適合我的,但如果你有煩惱 - here一些替代品。 也有[更多的解決方案] [2]在那裏當您使用API​​級別11和更高...

2)讓您的ImageView頂部位置

private void getPositionOfImageView(){     
     ImageView imageView = (ImageView) findViewById(R.id.imageView); 
     // Top position view relative to parent (Button and ImageView have same parent) 
     int topCoordinate = imageView.getTop(); 
     adjustButton(topCoordinate); 
    } 

3 。)添加或爲了調整按鈕與圖像對準

public void adjustButton(int topCoordinate){ 
     Button button = (Button) findViewById(R.id.button); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params.topMargin = topCoordinate; 
     button.setLayoutParams(params); 
    } 

此步驟將是通過使用API​​ 11平滑:button.setTop(topCoordinate)

當然,你可以縮短它的所有內容,並把它放在一個單調的方法中,只是認爲3個步驟更好解釋。希望代碼有助於開始!

+0

非常感謝,這很好! –

1

U可以使用的LinearLayout爲如下顯示圖像&按鈕:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
      android:id="@+id/longText" 
      android:gravity="center" 
     android:text="Some very long text" /> 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:id="@+id/subtitle" 
     android:layout_below="@+id/longText" 
     android:text="subtitle" /> 
     <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_below="@+id/subtitle" 
       android:layout_toLeftOf="@+id/subtitle" 
       android:layout_height="wrap_content" 
       android:text="button" /> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
       android:layout_toRightOf="@+id/button1" 
     android:layout_below="@+id/subtitle" 
     android:orientation="horizontal" 
      > 


      <ImageView 
       android:id="@+id/imageView2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 

     </LinearLayout> 

</RelativeLayout> 
+0

對不起,我忘了解釋Button _has_放在LinearLayout之外,因爲後者將成爲ViewPager的一部分。我用這些信息更新了我的問題。 –

相關問題