【问题标题】:Android center of view in other view [closed]其他视图中的Android视图中心[关闭]
【发布时间】:2014-07-27 06:00:08
【问题描述】:

我想像上图那样定位蓝色方块,蓝色视图的中心在另一个视图的角落。该怎么做?

【问题讨论】:

  • 从您的问题中不清楚您是在 xml 还是 canvas 中使用布局。如果你使用的是canvas,那么可以通过view的onDraw()方法中的一些计算和使用canvas来轻松完成。

标签: android view position center


【解决方案1】:

试试这个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
android:focusable="true" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="60dip"
    android:layout_height="60dip"
    android:layout_marginTop="8dp"
    android:background="@drawable/image_border"
    android:contentDescription="image"
    android:scaleType="center" />


<TextView
    android:id="@+id/txt_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="-10dip"
    android:layout_toRightOf="@+id/icon"
    android:background="@drawable/badge_count2"
    android:contentDescription="badge"
    android:gravity="center"
    android:text="1"
    android:textColor="@color/White"
    android:textStyle="bold"
    android:visibility="visible" />

</RelativeLayout>

输出:

您只需要根据您的要求将TextView 更改为ImageView

【讨论】:

    【解决方案2】:

    有多种解决方案可以使用RelativeLayout 或FrameLayout。

    看看RelativeLayout:http://developer.android.com/reference/android/widget/RelativeLayout.html

    尝试类似:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
    <TheRedView
        android:id="@+id/idRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="half of the idBlue width"
        android:layout_marginRight="half of the idBlue height"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    
    <TheBlueView
        android:layout_width="fixed width"
        android:layout_height="fixed width"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多