【问题标题】:Textview in center below of imageview Android位于imageview Android下方中心的Textview
【发布时间】:2018-08-31 05:27:04
【问题描述】:

今天过得怎么样。

我想知道我今天卡在哪里。我在列表中获取图像和文本的 json,我的图像是圆形的。当我得到带有文本的图像时,它看起来很糟糕,因为文本不在 imageview 下方的中心。对此进行了很多搜索,但我没有找到。我只是在父级“true”中找到了重力“中心”和中心,我尝试了这两个但我没有得到解决方案。我的代码是:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity>


<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginTop="70dp"
    android:layout_marginStart="15dp"
    android:id="@+id/img_design" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_design"
    android:id="@+id/design_color"
    android:text=" "
    android:layout_centerInParent="true"
    android:textStyle="bold"
    android:textSize="15sp" />
</RelativeLayout>

我想在图像中间自动设置这样的文本,因为文本的长度不是固定的:

         IMAGEVIEW
           text

请指导我,建议我并解决这个问题,如果您对我的问题有任何疑问,请询问我。谢谢。

【问题讨论】:

  • 因为它在 UI 上,请附上一张显示问题的图片,可能还有一张显示您想要完成的操作
  • 先生,它完成了谢谢你尝试解决我的问题。

标签: android android-layout android-imageview textview


【解决方案1】:

使用这个我希望它会像魅力一样工作。像下面一样为 imageview 和 textview 创建和分离父级并尝试.. 使用你自己正在使用的圆形图像视图

在此处输入代码//schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="wrap_content">


<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/img_design"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:src="@color/white"
      />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
     android:layout_centerInParent="true"
    android:layout_below="@+id/img_design" />
</RelativeLayout>

【讨论】:

    【解决方案2】:

    您可以用下面的 XML 代码替换您的布局。

    这里不需要额外的布局。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">
    
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/img_design"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="10dp"
        tools:src="@drawable/profile_sidemenu"/>
    
    <TextView
        android:id="@+id/design_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_below="@+id/img_design"
        android:layout_centerInParent="true"
        tools:text="This is sample text"
        android:textSize="15sp"
        android:textStyle="bold" />
    </RelativeLayout>
    

    输出:

    【讨论】:

    • @GouravManuja 它的工作,因为根相对布局宽度是 android:layout_width="wrap_content" 再次检查
    • 但是先生,我不需要更改 Textview 我这样写就行了,所以不要做复杂到您的代码。谢谢先生
    【解决方案3】:

    仅查看代码,_android:layout_centerInParent_ 对我来说毫无意义,而您希望将文本水平居中。此外,如果您希望圆形图像和文本都成为以它们的父级为中心的组,您应该再添加一个环绕两者的布局,并将其在父级中居中。

    【讨论】:

      【解决方案4】:

      尝试将&lt;CircleImageView&gt;&lt;TextView&gt; 包装成垂直方向的&lt;LinearLayout&gt;,并居中&lt;LinearLayout&gt;

      【讨论】:

        【解决方案5】:

        试试这个

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/img_design"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        
            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:text="TextView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/img_design" />
        </android.support.constraint.ConstraintLayout>
        

        【讨论】:

          【解决方案6】:

          如果您将宽度保持为wrap_content 给您的亲戚,并将android:layout_centerHorizontal="true" 用于TextView 而不是父级的中心,您的布局将起作用。如果你想要全屏,那么使用下面的布局

           <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          
              <LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
          
                  <de.hdodenhof.circleimageview.CircleImageView
                      android:id="@+id/img_design"
                      android:layout_width="110dp"
                      android:layout_height="110dp"
                      android:layout_marginStart="15dp"
                      android:layout_marginTop="70dp" />
          
                  <TextView
                      android:id="@+id/design_color"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:text=" "
                      android:textSize="15sp"
                      android:textStyle="bold" />
              </LinearLayout>
          </RelativeLayout>
          

          【讨论】:

          • 先生,我正在尝试自己的相对布局,并完成了谢谢尝试解决我的问题。我想 +1 对你的回答,但我不能引起 15 名声望。
          【解决方案7】:

          试试下面的 XML 代码

          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:orientation="vertical"
          android:layout_height="match_parent"
          tools:context=".MainActivity">
          
          <de.hdodenhof.circleimageview.CircleImageView
              android:id="@+id/img_design"
              android:layout_width="110dp"
              android:layout_gravity="center"
              android:layout_marginTop="10dp"
              android:layout_height="110dp" />
          <TextView
              android:id="@+id/design_color"
              android:layout_width="wrap_content"
              android:layout_gravity="center"
              android:layout_height="wrap_content" />
          
          </LinearLayout> 
          

          【讨论】:

          • @Gaurav Manuja.. 没关系。如果答案对您也有帮助,请为答案投票
          • 先生,我不能引起 15 名声望,请 +1 对我的问题,以提高我的声望。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-26
          相关资源
          最近更新 更多