1

Я хочу создать динамически несколько кнопок с меткой и другую маленькую кнопку на нем. Например: -
enter image description hereenter image description hereКак добавить кнопку изображения и ярлык на главной кнопке динамически в Android?

+0

Создание пользовательского вида путем расширения 'View' и нарисовать на холсте. –

+0

Можете ли вы предоставить графический интерфейс? –

ответ

1

Нужно добавить вытяжке в кнопку right.I думаю, нет необходимости добавлять дополнительные кнопки на кнопку, если не будет предпринято никаких действий для верхней кнопки. смотрите ниже код и попробуйте

LinearLayout parent = new LinearLayout(this); 

     parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     parent.setOrientation(LinearLayout.HORIZONTAL); 
     for (int i = 0 ; i < 10 ; i++) { 
      Button b = new Button(this); 
      b.setText("Primary"); 
      Drawable image = ContextCompat.getDrawable(getApplicationContext(), R.drawable.your_image); 
      image.setBounds(0, 0, 60, 60); 
      b.setCompoundDrawables(null, null, image, null); 
      parent.addView(b); 
     } 

Если вы столкнулись с какой-либо вопрос .. дайте мне знать .. надеюсь, что это поможет

+0

спасибо! Это сработало для меня. –

0

ИТАК первую очередь сделать элемент изображения кнопки и этикетки в формате XML.

item.xml

<LinearLayout 
    android:id="@+id/llOverallTitleDOWN" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

Затем на Java файл см ниже код.

LinearLayout llOverallTitleDOWN = (LinearLayout) findViewById(R.id.llOverallTitleDOWN); 

    View layout2 = LayoutInflater.from(this).inflate(R.layout.item, llOverallTitleDOWN, false); 

    ImageButton imgBtn = (ImageButton) layout2.findViewById(R.id.imgBtn); 
    TextView textLabel = (TextView) layout2.findViewById(R.id.textLabel); 
    imgBtn.setImageBitmap(bitmap); 
    textLabel.setText("label"); 

    llOverallTitleDOWN.addView(layout2); 
Смежные вопросы