Android:用户选择textView背景的颜色
我正在尝试创建一个菜单选项,用户可以在其中选择文本视图可以显示的颜色。例如,用户选择红色,选择一个预览按钮,将textView背景设置为红色。如有任何建议,将不胜感激Android:用户选择textView背景的颜色,android,styles,spinner,Android,Styles,Spinner,我正在尝试创建一个菜单选项,用户可以在其中选择文本视图可以显示的颜色。例如,用户选择红色,选择一个预览按钮,将textView背景设置为红色。如有任何建议,将不胜感激 public class UserMenu extends Activity implements OnClickListener { Button preview; Spinner spinnerColor; @Override public void onCreate(Bundle savedInstanceS
public class UserMenu extends Activity implements OnClickListener {
Button preview;
Spinner spinnerColor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_menu);
spinnerColor = (Spinner) findViewById(R.id.spinnerColorMenu);
TextView Title = (TextView)findViewById(R.id.ViewModuleTitle);
preview = (Button)findViewById(R.id.previewButton);
preview.setOnClickListener(this);
}
public void onClick(View v)
{
String color = spinnerColor.getSelectedItem().toString();
Title.setBackgroundResource(R.color.color);
}
}
布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/ViewModuleTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/darkBlue"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="@string/addModule"
android:textColor="@color/white"
android:textSize="22dp" />
<TextView
android:id="@+id/lableTextModuleCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enterModuleCode"
android:layout_marginLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
style="@style/textColor"/>
<Spinner
android:id="@+id/spinnerColorMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/colorMenu"/>
<Button
android:id="@+id/previewButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="previewButton"
android:text="@string/addModule" />
</LinearLayout>
更多
更多
它不工作,您应该使用开关(颜色)
它不起作用,您应该使用
开关(颜色)
您可以尝试以下方法:-
<item>red</item>
<item>blue</item>
<item>green</item>
<item>black</item>
}
这个很好用。不需要任何if-else语句或任何开关
p.S.parseColor()方法支持#RRGGBB#AARRGGBB‘红色’、‘蓝色’、‘绿色’、‘黑色’、‘白色’、‘灰色’、‘青色’、‘洋红’、‘黄色’、‘浅灰色’、‘深灰色’格式。您可以尝试以下方法:-
<item>red</item>
<item>blue</item>
<item>green</item>
<item>black</item>
}
这个很好用。不需要任何if-else语句或任何开关
p.S.parseColor()方法支持#RRGGBB#AARRGGBB'红色'、'蓝色'、'绿色'、'黑色'、'白色'、'灰色'、'青色'、'洋红'、'黄色'、'浅灰色',“darkgray”格式。了解布局的外观会有帮助了解布局的外观会有帮助我尝试了类似于上面的方法,但是R.color给了我一个错误:无法解析为变量。是否要更改背景色?我尝试了类似于上面的方法,但是R.color给了我一个错误:无法已解析为变量。是否更改背景颜色?
<item>red</item>
<item>blue</item>
<item>green</item>
<item>black</item>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerColor = (Spinner) findViewById(R.id.spinnerColorMenu);
Title = (TextView)findViewById(R.id.ViewModuleTitle);
preview = (Button)findViewById(R.id.previewButton);
preview.setOnClickListener(this);
}
public void onClick(View v)
{
String color = spinnerColor.getSelectedItem().toString();
int parsed_color = Color.parseColor(color);
Title.setBackgroundColor(parsed_color );
}