如何在Android中的图像按钮下添加文本?

Xia*_*Han 8 android button imagebutton

我想在图像按钮下添加文本,这些文本也是可点击的,并且将被重定向到与图像按钮相同的活动.基本上它就像任何Android手机中的应用程序列表,例如在此输入图像描述

Ree*_*eed 12

如果你ImageButton在XML中声明了你的声明,那么只需将它放入一个LinearLayout包含a TextView并在其中设置onClickListenerLinearLayout.结构就像

<LinearLayout 
      ... > <!-- This is the LinearLayout for the xml document -->

    <!-- Some other layout code here if you please -->
    <LinearLayout 
       android:id="@+id/linLayout"
            ... >
         <ImageButton
               ... />
         <TextView
               ... />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后在你的java中:

LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout);
        layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
Run Code Online (Sandbox Code Playgroud)

如果你ImageButton通过java代码动态添加每个代码,那么它仍将保持相同的结构.如果我需要添加任何内容,请告诉我.

  • 那你做错了.如果包含特定项目的LinearLayout设置为侦听单击,则图像和正在侦听的文本.仅包含所有这些视图的布局. (2认同)

Sri*_*esh 8

setAlpha并执行以下操作

<Button
        android:id="@+id/comparePack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:onClick="compareButtonClick"
        android:text="@string/compare" 
         android:drawableTop="@drawable/compare1edit"
        android:gravity="left|center_vertical"/>
Run Code Online (Sandbox Code Playgroud)