如何定位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

任何想法?

您需要添加属性:

android:layout_alignBottom="@+id/tense_textview"

android:layout_alignTop="@+id/tense_textview"到你的imageview

使用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中有固有的填充,这阻止了我实现像素的完美。(这是可以做到的,只是很痛苦)

尝试设置ImageButton的高度为match_parent

试试这个-

<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>

最新更新