【问题标题】:How to center relative layout with its text and images?如何将相对布局与其文本和图像居中?
【发布时间】:2014-07-17 07:05:09
【问题描述】:

我有RelativeLayout,其中包含其他元素,例如 imageview、textview。当我以小屏幕尺寸(Galaxy S2)运行应用程序时,它显示为图像 1。但是当它在更大的屏幕(Nexus 7)上运行时,它显示为图像 2。我不想放大图像以获得更大屏幕,我希望它保持我设置的某些尺寸(参见 .xml)。但是我怎样才能让它出现在大屏幕的中心(如图 3 所示)。

感谢您的建议。

.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/stainbck"
android:orientation="vertical"
android:padding="25dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Customized Templates"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#eaeaea" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:text="1" />

    <Button
        android:id="@+id/btn2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btn1"
        android:layout_weight="1"
        android:text="2" />

    <Button
        android:id="@+id/btn3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btn2"
        android:layout_weight="1"
        android:text="3" />

    <Button
        android:id="@+id/btn4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="4" />
</LinearLayout>

<LinearLayout
    android:id="@+id/relative2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:text="5" />

    <Button
        android:id="@+id/btn6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:text="6" />

    <Button
        android:id="@+id/btn7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn6"
        android:layout_weight="1"
        android:text="7" />

    <Button
        android:id="@+id/btn8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn7"
        android:layout_weight="1"
        android:text="8" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/relative"
    android:layout_width="265dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:gravity="center"
    android:longClickable="true" >

    <ImageView
        android:id="@+id/blankcard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true" />

    <TextView
        android:id="@+id/crdCompany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Company"
        android:textColor="#20526d"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdWebsite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="16dp"
        android:text="website"
        android:textColor="#20526d"
        android:textSize="9sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/crdPhone"
        android:layout_alignParentLeft="true"
        android:text="address"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/crdWebsite"
        android:layout_alignBottom="@+id/crdWebsite"
        android:layout_alignParentRight="true"
        android:text="email"
        android:textColor="#20526d"
        android:textSize="9sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/crdWebsite"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="17dp"
        android:text="phone"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/crdCompany"
        android:text="name"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/crdName"
        android:layout_marginTop="12dp"
        android:text="title"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdMobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/crdTitle"
        android:layout_marginTop="5dp"
        android:text="mobile"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="25dp" >

        <ImageView
            android:id="@+id/crdlogoframe"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentLeft="true"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/border"
            android:visibility="visible" />
    </FrameLayout>

    <ImageView
        android:id="@+id/crdqrframe"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignTop="@+id/frameLayout1"
        android:layout_toRightOf="@+id/frameLayout1"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/border" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/bottomrltv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="10dp"
    android:gravity="bottom" >

    <Button
        android:id="@+id/btnsend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/view"
        android:layout_weight="1"
        android:text="Send" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/btnedit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btnsend"
        android:layout_toRightOf="@+id/view"
        android:layout_weight="1"
        android:text="Edit" />
</RelativeLayout>

注意:.xml文件展示了整个界面,不仅仅是RelativeLayout。

【问题讨论】:

  • 为此使用权重并为此创建 diffrent2 值文件夹
  • @rashi。您能否在回答中证明我有机会接受它作为答案并投票。谢谢。
  • 如果您在使用 android:centerInParent='true' 后使用主父视图作为相对布局,这样就可以了..请尝试一下..但是如果您想在相对视图中管理上下视图你用这个..
  • 我将 android:layout_centerInParent="true" 放入 imageView 和 inti relativeLayout 但不幸的是它不起作用。
  • @Umitk 只需创建 values 文件夹,并在 values 文件夹中为 diffrent2 大小提供 style.xml ..所以你会得到那个

标签: android android-imageview android-relativelayout centering


【解决方案1】:

如果我对您的理解正确,您需要在他的父 relativelayout 中将带有 id 空白卡的 imageview 居中?然后可以设置 android:centerInParent='true'...

【讨论】:

  • 这不起作用。但我想向你强调,这种相对布局也有 textviews 而不仅仅是 imageview。这就是为什么我不认为设置 imageview 中心可以单独解决这个问题。虽然我试过了。但没有用。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 2017-01-18
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多