如何在android中的按钮内放置一个可点击的图标

问题描述 投票:0回答:3

我想在全宽按钮的右侧放置一个设置图标,单击此按钮会打开一个警告对话框,并允许用户将声音设置为铃声或通知音。

我想在我的按钮右侧显示这些微小的可点击图标; enter image description here

我在这里先向您的帮助表示感谢

android android-imagebutton
3个回答
0
投票

尝试使用此布局并在ImageView上设置onClickListener您还可以在TextView上设置另一个clickListener以执行其他操作。它们一起看起来像一个按钮。

<RelativeLayout android:layout_width="wrap_content"
                android:layout_height="match_parent">

    <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_toLeftOf="@id/zxc"
              android:text="Example"/>

    <ImageView android:id="@+id/zxc"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:background="@drawable/ic_comment"
               android:layout_alignParentRight="true"/>
</RelativeLayout>

0
投票

XML

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_purple"
    >

    <Button
        android:id="@+id/share_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:drawableRight="@drawable/ic_payment"/>

    <TextView
        android:id="@+id/share_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is  a samplt text"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toLeftOf="@id/share_btn"/>

</RelativeLayout>

活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button mShareButton;
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mShareButton = (Button)findViewById(R.id.share_btn);
    mTextView = (TextView)findViewById(R.id.share_txt);
    mShareButton.setOnClickListener(this);
    mTextView.setOnClickListener(this);
}

@Override
public void onClick(View view) {

    switch (view.getId())
    {
        case R.id.share_btn:
            Log.i("Test"," Button clicked");
            break;

        case R.id.share_txt:
            Log.i("Test"," Text clicked");
            break;
    }
  }
}

0
投票

我终于通过使用线性布局将其设置为水平和权重总和为10来工作。在线性布局中,我创建了一个宽度为匹配父级和layout_weight为10的按钮,然后为小铃铛图标创建了一个ImageButton将width设置为包含内容并将layout_weight设置为0.我必须将按钮和ImageButton的高度和背景设置为相同的50sp和#6465a5。谢谢大家的帮助。在这里完成初学者。 :d

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="10">


        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="50sp"
            android:layout_weight="10"
            android:background="#6465A5"
            android:fontFamily="@font/shadowsintolight"
            android:gravity="fill"
            android:onClick="translate"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:tag="themostamazingthings"
            android:text="THE MOST AMAZING THINGS"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size"/>

        <ImageButton
            android:id="@+id/bell1"
            android:layout_weight="0"
            android:layout_width="wrap_content"
            android:layout_height="50sp"
            android:paddingRight="10dp"
            android:src="@drawable/bell"
            android:background="#6465A5"
            android:scaleType="fitCenter"
            android:onClick="bellClick"
            android:tag="themostamazingthings"/>

    </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.