每边有两种颜色的按钮
Button with two colors on each side
是否可以用这种配色方案制作一个自定义按钮,其中一种颜色在右侧,另一种颜色在左侧?
在我之前的搜索中,我只能找到如何制作按钮渐变色,这不是我需要的那种颜色
是否可以根据我提供的示例将按钮制作成两种颜色并排的颜色?
您可以使用 LinearLayout 作为按钮以更复杂的方式完成此操作。
简单的例子:
XML 带有 LinearLayout 按钮的文件:
<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 表示位于您的 drawable 目录中的图像。
按钮所在的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;
}
}
}
并且输出:
<?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>
是否可以用这种配色方案制作一个自定义按钮,其中一种颜色在右侧,另一种颜色在左侧?
在我之前的搜索中,我只能找到如何制作按钮渐变色,这不是我需要的那种颜色
是否可以根据我提供的示例将按钮制作成两种颜色并排的颜色?
您可以使用 LinearLayout 作为按钮以更复杂的方式完成此操作。 简单的例子:
XML 带有 LinearLayout 按钮的文件:
<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 表示位于您的 drawable 目录中的图像。
按钮所在的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;
}
}
}
并且输出:
<?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>