【问题标题】:Remove Padding between drawableTop and text删除 drawableTop 和 text 之间的 Padding
【发布时间】:2017-04-04 14:26:16
【问题描述】:

我有 CustomLayout,它有 TextView,我在其上以编程方式设置文本和可绘制但问题是当我设置可绘制对象时,它添加了一个巨大的填充,而我想要绘制的内容更多地位于中心而不是顶部.

自定义视图组

public class ProfileGridView extends FrameLayout {
public ProfileGridView(@NonNull Context context) {
    super(context);
    initView(context,null);
}

public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView(context,attrs);

}

public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context,attrs);
}

@TargetApi(21)
public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initView(context,attrs);
}

public void initView(Context context, AttributeSet attrs) {
    TextView gridTextView;
    View view = inflate(context, R.layout.square_grid, null);
    gridTextView = (TextView) view.findViewById(R.id.grid_description);

    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProfileGridView);
    Drawable drawable = array.getDrawable(R.styleable.ProfileGridView_drawble);
    String description = array.getString(R.styleable.ProfileGridView_grid_description);

    gridTextView.setText(description);
    gridTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
    addView(view);
    array.recycle();
   }
}

自定义视图布局文件

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:gravity="center"
android:padding="0dp"
android:textSize="20sp" />

MainActivit.xml

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

  <com.example.views.ProfileGridView
      android:layout_width="200dp"
      android:layout_height="200dp"
      app:drawble="@drawable/ic_message"
      app:grid_description="Message"/>

</RelativeLayout>

【问题讨论】:

    标签: android android-custom-view


    【解决方案1】:

    你能像这样使用 CustomView LayoutFile 吗?

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
    <ImageView
        android:id="@+id/image"
        android:layout_width="52dp"
        android:layout_height="52dp" />
    <TextView
        android:id="@+id/grid_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="4dp"
        android:textSize="20sp"/>
    </LinearLayout>
    

    然后像这样设置drawable

        ImageView image;
        image = (ImageView) view.findViewById(R.id.image);
        image.setImageDrawable(drawable);
    

    代替

    gridTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
    

    编辑

    如你所问,你可以让 ProfileGridView wrap_content 而不是 200dp。使用 setCompoundDrawablesWithIntrinsicBounds 可以将可绘制对象附加到 TextView (200dp) 的边界。 [您可以查看文档了解更多信息](https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable))

    【讨论】:

    • 我想这是唯一的方法,但我没有使用 LinearLayout,而是使用 TextView 来设置 ImageDrawable 顶部
    • 通过攻击可绘制边界,您的意思是将 TextView 高度设置为 200dp 并将 wrap_content 添加到它在 activity_main 中的高度
    • @prathamkesarkar 抱歉,附上。并将高度和宽度设置为 wrap_content。
    【解决方案2】:

    这不是填充问题,是因为您将可绘制的宽度和高度设置为 200dp

    你可以在drawable文件中使用“wrap_content”吗?

    如果不只是有一个图像文件并将其设置为 TextView 的可绘制对象

    【讨论】:

    • 只要尝试将宽度和高度设置为 40dp 之类的,你会发现不是填充
    • 我尝试将 textview 中的高度设置为 64dp,但它仍然向我显示相同的东西,似乎 drawable 正在尝试使用所有可用空间
    • 请尝试更改 Drawable RelativeLayout 的宽度,从 match_parent 到 wrap_content 甚至你说的 64dp
    • 为什么文本和drawable之间的空间宽度
    • 在尝试以编程方式设置属性时,Textview 中似乎存在一些错误
    【解决方案3】:

    将 drawableBottom 设置为相同大小的图像将使文本居中。这是一个简单的可绘制对象:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <size android:width="24dp"
            android:height="24dp"/>
    </shape>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-11
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2013-01-13
      • 2018-11-10
      相关资源
      最近更新 更多