我的左侧有几个带有可绘制图标的TextView。 布局:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/card_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/detail_padding">
<TextView
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_location"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_location" />
<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_status"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_status"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_description"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_description"/>
</LinearLayout>
</android.support.v7.widget.CardView>
截图:
如果文字太大,图标就会显示在textview的中心。我想在textview上设置图标对齐顶部。有可能吗?
答案 0 :(得分:0)
您必须将布局保持为RelativeLayout
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/card_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/detail_padding">
<TextView
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_location"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_location" />
<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_status"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_status"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_description"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_description"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 1 :(得分:0)
您可以设置
android:layout_toRightOf="@+id/imageId"
和
android:layout_alignParentLeft="true"
在图像上。
希望这会有所帮助。
答案 2 :(得分:0)
只需使用ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/detail_padding">
<TextView
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_location"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_location" />
<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin"
android:drawableLeft="@drawable/ic_status"
android:drawablePadding="@dimen/drawable_padding"
android:drawableStart="@drawable/ic_status" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/drawable_padding"
android:src="@drawable/ic_description" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/title_margin" />
</LinearLayout>
</LinearLayout>
答案 3 :(得分:0)
为此,您必须创建一个包装Drawable的自定义Drawable,然后通过覆盖onDraw(Canvas)方法来操纵自定义Drawable的绘图。
下面有一个示例。这会将图像对齐到顶部,但您也可以通过在onDraw(Canvas)方法中实现所需的逻辑使其与TextView的底部,左侧或右侧对齐。您可能还希望在onDraw(Canvas)中构建一个边距,以使您的设计实现像素完美。
样本用法:
GravityTopDrawable gravityDrawable = new GravityTopDrawable(innerDrawable);
// NOTE: next 2 lines are important!
innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight());
mTextView.setCompoundDrawables(gravityDrawable, null, null, null);
示例代码:
public class GravityTopDrawable extends Drawable {
// inner Drawable
private final Drawable mDrawable;
public GravityTopDrawable(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();
}
}