2013-01-13 87 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的一部分。我用这些信息更新了我的问题。 –

相关问题