我需要在TextView上显示3个可绘制对象。我已经尝试过使用自定义视图,并且它可以工作。但是,随着我对LinearLayout的扩展,我无法直接在xml上使用TextView属性,如textColor,textSize等。可以做TextView的扩展吗?这是我的代码:
verified_component.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/txtUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray_333333"
android:gravity="center_vertical"
android:lines="1"
android:maxWidth="200dp"
android:textSize="16sp"
android:ellipsize="end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iconVerified"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintTop_toTopOf="parent"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/iconVerified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
app:layout_constraintBottom_toBottomOf="@+id/txtUserName"
app:layout_constraintStart_toEndOf="@+id/txtUserName"
app:layout_constraintEnd_toStartOf="@+id/iconOwner"
app:layout_constraintTop_toTopOf="@+id/txtUserName"
app:srcCompat="@drawable/verificado" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/iconOwner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="@+id/txtUserName"
app:layout_constraintStart_toEndOf="@+id/iconVerified"
app:layout_constraintEnd_toStartOf="@+id/iconAdmin"
app:layout_constraintTop_toTopOf="@+id/txtUserName"
app:srcCompat="@drawable/badge_owner" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/iconAdmin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="@+id/txtUserName"
app:layout_constraintStart_toEndOf="@+id/iconOwner"
app:layout_constraintTop_toTopOf="@+id/txtUserName"
app:layout_constraintEnd_toStartOf="@+id/iconModerator"
app:srcCompat="@drawable/badge_adm" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/iconModerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="@+id/txtUserName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/iconAdmin"
app:layout_constraintTop_toTopOf="@+id/txtUserName"
app:srcCompat="@drawable/badge_mod" />
</android.support.constraint.ConstraintLayout>
VerifiedTextView.java
package br.com.hotmart.hifire.v2.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import br.com.hotmart.hifire.R;
public class VerifiedTextView extends LinearLayout {
View layout = null;
TextView textViewUserName = null;
AppCompatImageView iconVerified = null;
AppCompatImageView iconOwner = null;
AppCompatImageView iconAdmin = null;
AppCompatImageView iconModerator = null;
Context context = null;
public VerifiedTextView(Context context) {
super(context);
this.context = context;
}
public VerifiedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerifiedTextView);
String userName = a.getString(R.styleable.VerifiedTextView_userName);
userName = userName == null ? "" : userName;
boolean isVerified = a.getBoolean(R.styleable.VerifiedTextView_isVerified, false);
boolean isOwner = a.getBoolean(R.styleable.VerifiedTextView_isOwner, false);
boolean isAdmin = a.getBoolean(R.styleable.VerifiedTextView_isAdmin, false);
boolean isModerator = a.getBoolean(R.styleable.VerifiedTextView_isModerator, false);
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater) getContext().getSystemService(service);
if (li != null) {
layout = li.inflate(R.layout.verified_component, this, true);
textViewUserName = layout.findViewById(R.id.txtUserName);
iconVerified = layout.findViewById(R.id.iconVerified);
iconOwner = layout.findViewById(R.id.iconOwner);
iconAdmin = layout.findViewById(R.id.iconAdmin);
iconModerator = layout.findViewById(R.id.iconModerator);
setText(userName);
setIsVerified(isVerified);
setIsOwner(isOwner);
setIsAdmin(isAdmin);
setIsModerator(isModerator);
}
a.recycle();
}
public VerifiedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
@SuppressWarnings("unused")
public void setText(String text) {
textViewUserName.setText(text);
}
@SuppressWarnings("unused")
public void setIsVerified(boolean isVerified) {
iconVerified.setVisibility(isVerified ? View.VISIBLE : View.GONE);
}
@SuppressWarnings("unused")
public void setIsOwner(boolean isOwner) {
iconOwner.setVisibility(isOwner ? View.VISIBLE : View.GONE);
}
@SuppressWarnings("unused")
public void setIsAdmin(boolean isAdmin) {
iconAdmin.setVisibility(isAdmin ? View.VISIBLE : View.GONE);
}
@SuppressWarnings("unused")
public void setIsModerator(boolean isModerator) {
iconModerator.setVisibility(isModerator ? View.VISIBLE : View.GONE);
}
}
使用示例:
<com.example.VerifiedTextView
android:id="@+id/text_name_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:userName="Lorem Ipsum"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
答案 0 :(得分:0)
您可以编写View的构造函数,以从传入的Attributes中读取这些属性,然后调用内部文本视图以设置这些属性。