【问题标题】:Placing imagebuttons with text inside linear layout在线性布局内放置带有文本的图像按钮
【发布时间】:2016-10-14 16:17:25
【问题描述】:

我需要在屏幕顶部放置 5 个imageButtons。我是这样用LinarLayout 做到的:

<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="com.example.terrormachine.swipeapp.NewAdFragment">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/image"
            android:id="@+id/imageButton6"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/image"
            android:id="@+id/imageButton7"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/image"
            android:id="@+id/imageButton8"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/image"
            android:id="@+id/imageButton9"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/image"
            android:id="@+id/imageButton10"
            android:layout_weight="1"
            android:scaleType="fitCenter" />
    </LinearLayout>
</RelativeLayout>

问题是我需要在每张图片下方居中放置文本。我尝试了几种解决方案,但似乎没有一个对我有用。 我试过这个:

Android Text Below Icon (button) How to add a text under the image button in Android? How to place text below a Button or a ImageButton?

其中一些可以解决问题,但图像没有缩放。就我而言,我无法在下面设置文本。有没有办法做到这一点? 我不必使用LinearLayout,但我想不出任何适当的方法来将它们等距放置并具有屏幕宽度。欢迎任何想法。

这是我的目标:

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    只需将您的图像按钮和文本视图包装在线性布局中,如下所示:

    <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1">
                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:srcCompat="@drawable/image"
                        android:id="@+id/imageButton6"
    
                        android:scaleType="fitCenter" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Main"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"/>
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1">
                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:srcCompat="@drawable/image"
                        android:id="@+id/imageButton7"
    
                        android:scaleType="fitCenter" />
    
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Main"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"/>
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1">
                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:srcCompat="@drawable/image"
                        android:id="@+id/imageButton8"
    
                        android:scaleType="fitCenter" />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Main"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"/>
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1">
                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:srcCompat="@drawable/image"
                        android:id="@+id/imageButton9"
    
                        android:scaleType="fitCenter" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Main"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"/>
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1">
                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:srcCompat="@drawable/image"
                        android:id="@+id/imageButton10"
    
                        android:scaleType="fitCenter" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Main"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"/>
                </LinearLayout>
    
    
    
    
    
    
            </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      应该有几种方法可以做,我能想到的第一件事是代替 ImageViews,您可以使用 TextViews 并在每个 TextView 中为您的图像指定 drawableTop:

          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:drawableTop="@drawable/image" 
              ...etc  />
      

      否则,如果您需要 ImageViews,您可以将 ImageViews 和 TextViews 包装成单独的 Linears 以获得对齐:

      <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="com.example.terrormachine.swipeapp.NewAdFragment">
      
          <LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:layout_alignParentTop="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true">
      
              <LinearLayout
                  android:orientation="vertical"
                  android:layout_width="wrap_parent"
                  android:layout_height="wrap_parent" >
      
                  <ImageButton />
                  <TextView />
              </LinearLayout>
      
              <LinearLayout
                  android:orientation="vertical"
                  android:layout_width="wrap_parent"
                  android:layout_height="wrap_parent" >
      
                  <ImageButton />
                  <TextView />
              </LinearLayout>
          </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        不要考虑向图像按钮添加文本。

        考虑将图像添加到文本按钮:

                    <Button
                        android:id="@+id/imageButton9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableTop="@drawable/image"
                        android:text="Main"/>
        

        使用drawablePadding 和其他设置,您可以调整按钮以显示您想要的样子。要摆脱按钮边界,请使用android:background="@null"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-20
          • 1970-01-01
          • 2012-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多