【问题标题】:Android - How to display 4 text views with Icons?Android - 如何使用图标显示 4 个文本视图?
【发布时间】:2013-05-21 08:29:48
【问题描述】:

我想显示 4 个文本,其中的图像与此屏幕截图中显示的完全一样:

注意图片底部的 4 个文本,旁边有图标。

我的问题是我该怎么做,我的意思是,如何在每个文本字段上附加一个图标并在页面底部对称显示。

我想用图标来定位我的文本,就像这个例子一样。

感谢您的帮助,这对新手来说真的是一个很棒的练习:)

Dvir

【问题讨论】:

  • 几分钟前你刚刚发布了一个类似的问题,上次你要求文本视图,现在你也想要图像。你为什么不先尝试一下。用户已回答您的问题。它足以让您继续前进并开始自己做。你不能只问直接的解决方案。

标签: android image position


【解决方案1】:

为此使用drawableLeft

<TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/icon_1"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="24dip"
        android:maxLines="1"
       />

在java代码中

TextView textView = (TextView) findViewById(R.id.yourTV);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_1, 0, 0, 0);

【讨论】:

    【解决方案2】:

    您可以只使用TextView,并添加例如android:drawableLeft="@drawable/your_icon" 根据您想要图标的位置使用左/右。

    【讨论】:

      【解决方案3】:
      <LinearLayout
      
           android:orientation="vertical"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
      
          <LinearLayout
                  android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content">
      
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Text"
                      android:layout_weight="1"
                      android:drawableLeft="@drawable/icon"
                      android:gravity="center_horizontal"/>
      
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Text"
                      android:layout_weight="1"
                      android:drawableLeft="@drawable/icon"
                      android:gravity="center_horizontal"/>
          </LinearLayout>
      
          <LinearLayout
                  android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
      
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Text"
                      android:layout_weight="1"
                      android:drawableLeft="@drawable/icon"
                      android:gravity="center_horizontal"/>
      
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Text"
                      android:layout_weight="1"
                      android:drawableLeft="@drawable/icon"
                      android:gravity="center_horizontal"/>
          </LinearLayout>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        实现这一点的理想方法是:

        textView.setCompoundDrawables(getDrawable(R.drawable.image), 15), null, null, null);
        

        将以下函数也添加到您的代码中:

        public Drawable getDrawable(int drawable, float form_factor) {
            Drawable top = getApplicationContext().getResources().getDrawable(drawable);
            top.setBounds(0, 0, (int)form_factor, (int)form_factor);
            return top;
        }
        

        通过这种方式,您可以适当地设置/选择/调整文本图标的大小(通过调整形状因子的值)这在通过 xml 完成时是不可能的

        注意:我已经为方形可绘制对象编写了上述函数,对于矩形图像,我们必须相应地执行setBounds

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-17
          • 2017-11-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多