如何将android:drawable设置为left和top?
我只是想在我的TextView
的顶部和左上角设置图标。 这是我的代码和输出分别为:如何将android:drawable设置为left和top?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Content"
android:drawableLeft="@drawable/icon" />
输出:
,但我想我的图标设置为顶部和左侧像这样的:
为此,你必须采取单独的ImageView这样在TextView的领域,你必须添加一行代码,机器人:layout_toRightOf =“@ + ID/ImageView的”: 为了更好地理解看下面的代码:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView"
android:text="this si fodfjkhsdhaffjsdfasdfjhsdfhjsdhfjhsdfhsdhfhdsjfhsdjhfjhdsfhsdhfjhsdjhfjsdhfjhsdjfhjsdhfjhsdjfhjdshfsdjhfjsdhfsdkjhfjsdhfjhsdjfhjsdhjfhsdjhfjsdhfjhjsdhfjsdhjfhsdjf"
/>
</RelativeLayout>
完美的答案.. –
thnx @komalakhani –
感谢您的反馈@ DeepakRana –
您可以使用setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
(为了更好地理解是指该链接:)下面代码
https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableTop
看起来像完美的答案 –
它不能做到接近我的解决方案,因为当我设置顶部和左侧值我的图标它复制这个图标的两个版本... –
如果你给你的XML文件 –
使用;
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Content"
android:drawableTop="@drawable/def"
android:drawableLeft="@drawable/icon" />
试试这个
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello world"
android:layout_toEndOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_error" />
</RelativeLayout>
嵌套布局不利于演出。改为使用RelativeLayout。或者一个GridView,一个TableLayout,......任何事情都可以让你不将LinearLayouts嵌套在另一个中。 –
@ModularSynth检查更新ans –
我会把ImageView(它实际上是一个)之前。然后把TexView相对于它。但是,你会得到@ deepakrana的答案。 –
您可以对齐复合抽拉顶端(或底部)通过创建一个自定义Drawable来包装Drawable。
使用
GravityCompoundDrawable gravityDrawable = new GravityCompoundDrawable(innerDrawable);
// NOTE: next 2 lines are important!
innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
textView.setCompoundDrawables(gravityDrawable, null, null, null);
定制GravityCompoundDrawable类:
public class GravityCompoundDrawable extends Drawable {
// inner Drawable
private final Drawable mDrawable;
public GravityCompoundDrawable(Drawable drawable) {
mDrawable = drawable;
}
@Override
public int getIntrinsicWidth() {
return mDrawable.getIntrinsicWidth();
}
@Override
public int getIntrinsicHeight() {
return mDrawable.getIntrinsicHeight();
}
@Override
public void draw(Canvas canvas) {
int halfCanvas= canvas.getHeight()/2;
int halfDrawable = mDrawable.getIntrinsicHeight()/2;
// align to top
canvas.save();
canvas.translate(0, -halfCanvas + halfDrawable);
mDrawable.draw(canvas);
canvas.restore();
}
}
此代码将帮助您和见附件图像。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.90"
android:text="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using" />
</LinearLayout>
</LinearLayout>
哦,不...再次!表演嵌套布局不利。改为使用RelativeLayout。或者一个GridView,一个TableLayout,...无论如何都不允许将LinearLayouts嵌套在另一个中。 –
它不可能轻易与单一视图做到这一点,你可能需要使用的RelativeLayout和图像的单独ImageView的。 –
使用引力并设置它 –
@ jigarsavaliya你能解释更多吗? –