2015-10-20 30 views
0

我需要创建如下:如何将Android按钮放置在TextView之上?

---------------------------------------------------- 
|  |           | 
|  |           | 
---------------------------------------------------- 
    |      | 
    ImageButton    TextView 

的ImageButton的有图像,需要调整来适合的TextView的高度...

我已经成功地定位使用的RelativeLayout按钮:

 <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/tense_textview" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/tense" 
       android:gravity="center_horizontal" 
       android:background="@android:color/holo_orange_dark" 
       android:padding="15dp" 
       android:textStyle="bold" 
       android:textColor="@android:color/white" /> 
      <ImageButton 
       android:id="@+id/pauseButton" 
       android:background="@drawable/pause_button_background" 
       android:src="@drawable/pause" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" /> 
     </RelativeLayout> 

然而,这给了我:

      TextView 
          | 
---------------------------------------------------- 
|   |          | 
|   |          | 
|   |----------------------------------------- 
|   |          | 
---------------------------------------------------- 
    |      | 
    ImageButton   RelativeLayout 

有什么想法?

+0

简单,不要使用ImageView的。在TextView中使用**复合可绘制**。顺便说一句,你也可以通过这样做来摆脱无用的RelativeLayout。因此,在你的TextView中写入:'android:drawableLeft =“@ drawable/pause”' –

+0

虽然这不会影响图像的缩放... – lombrozo

+0

如果设置了TextView高度,将缩放到适合ImageView的高度wrap_content。 –

回答

0

使用的ImageView代替的ImageButton并添加
android:layout_alignBottom="@id/tense_textview" android:layout_alignTop="@id/tense_textview" android:scaleType="CENTER_CROP OR CENTER_INSIDE "

CENTER_CROP =缩放图像统一(保持即时通讯年龄的长宽比),以使图像的两个尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充)。

CENTER_INSIDE =均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)等于或小于视图的相应尺寸(减去填充)。

从个人经验来看,我从来不使用ImageButton,因为ImageView给了我更多的灵活性,并且ImageButton中存在固有的填充,使我无法实现像素完美。 (这是做,能够只是一个很大的痛苦)

+0

这压扁按钮,我需要它来保持它的长宽比... – lombrozo

+0

因此,对于textview,你想它被集中在图像按钮的右侧?意味着您将文本垂直和水平居中? –

+0

否我希望文本忽略该按钮,以便它位于父代的中间 – lombrozo

0

尝试将ImageButton的高度设置为match_parent

1

您需要添加属性:

android:layout_alignBottom="@+id/tense_textview" 

android:layout_alignTop="@+id/tense_textview" 您的ImageView

+0

这压扁按钮,我需要它来保持它的长宽比... – lombrozo

+0

是的,你可以添加android:scaleType =“CENTER_INSIDE”与上述解决方案。 –

0

试试这个 -

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/tense_textview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/tense" 
     android:gravity="center_horizontal" 
     android:background="@android:color/holo_orange_dark" 
     android:padding="15dp" 
     android:textStyle="bold" 
     android:textColor="@android:color/white" 
     android:layout_toRightOf="@+id/pauseButton" 
     android:layout_alignParentRight="true" 
     android:layout_alignBottom="@+id/pauseButton" 
     android:layout_alignTop="@+id/pauseButton"/> 
    <ImageButton 
     android:id="@+id/pauseButton" 
     android:background="@drawable/pause_button_background" 
     android:src="@drawable/pause" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" /> 
</RelativeLayout> 
相关问题