我想更改我的应用程序中的所有颜色文本.所以我编写了这段代码,并在清单中设置了我的主题:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textColor">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但只有TextView文本为白色.我希望Button和EditText也是白色的.
谁能帮我?先感谢您.
android colors android-theme android-edittext android-button
我尝试以编程方式将属性AlignParentRight设置为RelativeLayout中的按钮,但程序崩溃时出现NullPointerException.这是我的代码:
//add horizontal realative layout
RelativeLayout layouth = new RelativeLayout(this);
layouth.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//add textview for label
TextView t = new TextView(this);
t.setText(d.getName());
t.setTextSize(25);
t.setId(2);
t.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//add button
Button b = new Button(this);
b.setText(d.getName());
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.addRule(RelativeLayout.LEFT_OF, 2);
b.setLayoutParams(layoutParams);
//add textview and button to horizontal linear layout
layouth.addView(t);
layouth.addView(b);
//add horizontal linear layout to vertical linear layout
layoutv = (LinearLayout) findViewById(R.id.ProjectViewLinearID);
layoutv.addView(layouth);
Run Code Online (Sandbox Code Playgroud)
我的问题在哪里?该错误发生在程序行中layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
更新解决方案:
这段代码适合我:
// Creating a new RelativeLayout
RelativeLayout layouth = …Run Code Online (Sandbox Code Playgroud) android android-layout android-button android-relativelayout
我有一个按钮选择器,可以在按下按钮时更改按钮图像.我还为按钮被禁用时设置了图像.我尝试以编程方式禁用按钮,但禁用的按钮图像没有出现.我的button_selector是否正确?
<item android:drawable="@drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
<item android:drawable="@drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="@drawable/red_btn_bg_pressed" android:state_focused="true"/> <!-- focused -->
<item android:drawable="@drawable/red_btn_bg"/> <!-- default -->
Run Code Online (Sandbox Code Playgroud)
我在我的代码中使用mButton.setEnabled(false)来禁用该按钮
我在res文件夹中定义了以下文件.
styles_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
<item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
themes_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@style/_AppTheme"/>
<style name="_AppTheme" parent="Theme.AppCompat.Light">
<item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>
<item name="android:buttonStyle">@style/ButtonAppTheme</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
在我的Layout.xml文件中,我已经定义了我的按钮,如下所示
<Button
android:id="@+id/btnAddTitle"
android:layout_below="@id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_AddTitle"
android:layout_margin="20dp"
/>
Run Code Online (Sandbox Code Playgroud)
如果我将以下行添加到上面的按钮视图,
android:background="@style/ButtonAppTheme"
Run Code Online (Sandbox Code Playgroud)
应用程序崩溃说应该为background属性设置drawable资源.
所以我创建了下面的drawable文件 - abc.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="@drawable/apptheme_btn_default_pressed_holo_light" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/apptheme_btn_default_focused_holo_light" />
<item android:state_enabled="true" …Run Code Online (Sandbox Code Playgroud) 我想为这个形状制作一个按钮

有没有办法用xml做到这一点?喜欢设置一些点,在我的情况下,我有5 ..
android android-button android-shape material-design material-components-android
我是android编程的新手.如何更改按钮的颜色?
<Button
android:id="@+id/btn"
android:layout_width="55dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Button Text"
android:paddingBottom="20dp"/>
Run Code Online (Sandbox Code Playgroud) Button当我使用android:backgroundxml中的属性为其设置背景时,我遇到了视图问题.在设置背景之前,该按钮具有默认大小.但是,当我将颜色作为背景,这是挺大的,虽然我没有设置不同的width或者height在其上.
在将background属性设置为button之前,这是我的xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:id="@+id/btn_row_option_done"
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Edit"
android:id="@+id/btn_row_option_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center_horizontal"
android:text="Delete"
android:id="@+id/btn_row_option_delete"
android:background="@color/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
所以取消按钮是默认大小,如截图.
但是当我在这样的取消按钮上设置颜色时
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
然后按钮变得比默认大小更大,即使我没有使宽度或高度更大.
这是截图
如上所示,取消按钮变大了.其实我正在设置背景颜色.即使在设置背景颜色后,如何修复它以获得默认大小?
我想为android中的按钮设置一个圆角,并在选中时更改按钮颜色.我正在做以下事情.
绘制/ push_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@color/green"/>
<item android:state_focused="true" android:drawable="@color/green"/>
<item android:state_focused="false" android:drawable="@color/green"/>
<item android:state_pressed="false" android:drawable="@color/red"/>
<item android:drawable="@drawable/push_button_background"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
绘制/ push_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#3F4040"/>
<corners
android:radius="7dp"
/>
</shape>
Run Code Online (Sandbox Code Playgroud)
在代码中,我正在使用
android:background="@drawable/push_button"
Run Code Online (Sandbox Code Playgroud)
这里的问题是,选择和取消选择时按钮颜色设置正确.但是,圆角不起作用.
怎么做?如果我使用
android:background="@drawable/push_button_background"
Run Code Online (Sandbox Code Playgroud)
然后,圆角正在工作,但选择时按钮颜色更改不起作用
怎么实现这个?
我已经提到了这个链接.即使这样没有帮助!!

我想创建一个这样的页面.这7个按钮已经存在但是如果用户想要添加更多类别(按钮),那么他可以使用+按钮并使用-按钮删除.这个有什么想法或教程吗?
这个问题在此之前已经被问过,但答案是不正确的.我想在屏幕方向更改上旋转一些视图,但我想保持布局不变.它们应该根据当前定向(旋转90度,180度270或360度SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_PORTRAIT,SCREEN_ORIENTATION_REVERSE_LANDSCAPE,SCREEN_ORIENTATION_REVERSE_PORTRAIT).
这就是我想要实现的目标:
我提到的链接中的答案表明我应该创建一个新的不同布局layout-land.显然,这不是我想要的.我不想重新创建活动或更改布局方向.我只想旋转一些视图,并保持其他视图在方向更改时保持不变.
旋转特定视图与更改或重新创建整个布局(两者都在方向更改)之间存在巨大差异.
使用此链接中的答案,我将能够使用此方法获取当前屏幕方向:
public static int getScreenOrientation(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| …Run Code Online (Sandbox Code Playgroud) android rotation android-layout android-button android-orientation