【问题标题】:Button with two colors on each side每侧有两种颜色的按钮
【发布时间】:2016-06-20 11:00:12
【问题描述】:

是否可以使用这种配色方案制作自定义按钮,其中一种颜色在右侧,另一种颜色在左侧?

在我之前的搜索中,我只能找到如何使按钮渐变,这不是我需要的那种颜色

是否可以根据我提供的示例将按钮并排设置两种颜色?

【问题讨论】:

  • 改用 ImageButton。
  • 使用自定义视图并像处理按钮一样处理它
  • 在左侧可以使用android:drawableLeft="@drawable/ic_gift"
  • 您可以简单地使用 layerList 并将这些东西作为 xml 布局来执行。简单示例,请在此处查看答案:stackoverflow.com/questions/8780119/…

标签: android android-layout button customization android-custom-view


【解决方案1】:

您可以使用 LinearLayout 作为按钮以更复杂的方式进行操作。 简单的例子:

带有 LinearLayout 按钮的 XML 文件:

<LinearLayout android:id="@+id/sophisticated_button"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <LinearLayout
        android:background="#333333"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="1">

        <ImageView
            android:layout_width="66dp"
            android:layout_height="66dp"
            android:src="@drawable/present"/>

    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center"
        android:text="SEND A GIFT \n TO A FRIEND"/>

</LinearLayout>

其中 present 代表位于您的可绘制目录中的图像。

按钮所在的Activity如下所示:

public class MainActivity extends Activity implements View.OnClickListener {

    private LinearLayout buttonLinearLayout;

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

        buttonLinearLayout = (LinearLayout)findViewById(R.id.sophisticated_button);
        buttonLinearLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sophisticated_button:
                Toast.makeText(getApplicationContext(), "Sophisticated Button Pressed", Toast.LENGTH_LONG).show();
                break;
        }
    }
}

还有输出:

【讨论】:

  • 它对我有用,现在问题解决了,我可以继续我的工作,谢谢它工作得很好
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="@drawable/dialog_rounded"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="SUCCESS"
        android:textColor="@color/primaryTextColor"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:textColor="@color/secondaryTextColor"
        android:textSize="17sp"
        android:textStyle="normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/dialog_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/yes"
            android:textAllCaps="false"
            android:background="@drawable/button_proceed"
            android:textColor="@color/whiteColor"
            android:textSize="20sp"
            android:layout_marginRight="10dp"
            android:textStyle="bold"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/dialog_action_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no"
            android:textAllCaps="false"
            android:background="@drawable/color_red_reject"
            android:textColor="@color/whiteColor"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_weight="1"/>


    </LinearLayout>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 2012-02-05
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2015-03-11
    • 2018-12-22
    相关资源
    最近更新 更多