объявляемый стиль drawableTop - PullRequest
       1

объявляемый стиль drawableTop

0 голосов
/ 11 февраля 2012

Я пытаюсь реализовать составное представление для своей деятельности на панели инструментов.Это Button с TextView.Нужна моя тема.У меня attr.xml файл

<declare-styleable name="DashboardButton">
    <attr name="drawableTop" format="reference"/> <!-- i want set android:drawableTop of button-->
    <attr name="btnText" format="string" /> <!-- text about button functionality-->
    <attr name="tvText" format="string" /> <!-- additional information -->
</declare-styleable>

также есть dashboard_button.xml макет

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btn"
        style="@style/DashboardButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView 
         style="@style/btn_add_info"
         android:id="@+id/txtView"
         android:text="0"/>
</RelativeLayout>

также есть пользовательский компонент DashboardButton

public class DashboardButton extends RelativeLayout {

private TextView txtView;
private Button btn;

public DashboardButton(Context context) {
    super(context);
}

public DashboardButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.dashboard_button, this);
    loadViews(attrs);
}

public DashboardButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.dashboard_button, this);
    loadViews(attrs);
}

private void loadViews(AttributeSet attrs) {
    txtView = (TextView) findViewById(R.id.txtView);
    btn = (Button) findViewById(R.id.btn);
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DashboardButton);
    txtView.setText(a.getString(R.styleable.DashboardButton_tvText));
    btn.setText(a.getString(R.styleable.DashboardButton_btnText));
   //i think issue here but can't understand what i should do
    Drawable topDrawable = a.getDrawable(R.styleable.DashboardButton_drawableTop); 
    btn.setCompoundDrawables(null, topDrawable, null, null);
    a.recycle();
}

public void setTxtViewText(CharSequence text) {
    txtView.setText(text);
}
}

и, наконец, у меня есть

    <my.package.name.DashboardButton
         android:id="@+id/Btn"
         style="@style/DashboardButton"
         app:btnText="@string/my_function"
         app:tvText="@string/add_info"
         app:drawableTop="@drawable/function_logo">
    </my.package.name.DashboardButton>

Все работает нормально, кроме app:drawableTop отрисовка не загружается во время выполнения, я не вижу отрисовки.Можете ли вы помочь мне советом?

1 Ответ

2 голосов
/ 12 апреля 2012

Вам необходимо установить границы для вашего рисования:

topDrawable.setBounds( new Rect( 0, 0, w, h ) );

или использовать альтернативно:

btn.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...