我在布局上有两个按钮,在大屏幕设备(平板电脑)上,我想限制它们的宽度,这样它们看起来并不荒谬.我希望使用maxWidth属性,但它在我的场景中显然没有任何作用.这是布局定义 - 按钮使用布局的整个宽度,忽略maxWidth中的任何值.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 2"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
为什么,以及如何解决这个(显然是疯狂的)限制?
如果我快速点击我的Android应用程序中的按钮,它背后的代码似乎运行两次.如果我单击我的菜单按钮两次,必须启动的活动onclick只启动两次,我必须从它退出两次.
这真的很烦人,因为如果我点击太快的菜单按钮我可以在后台加载一大堆活动,我必须逐个退出它们,所以这显然是我的应用程序的错误状态我想解决这个问题.
我该怎么办这个问题?
我使用简单的onClickListeners和Buttons
编辑:
关于答案和评论,我的菜单按钮如下所示:
top20Button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
favButton.setClickable(false);
nearButton.setClickable(false);
highlightedButton.setClickable(false);
top20Button.setClickable(false);
Intent i = new Intent();
i.putExtra("showDialog", false);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setClass(Search.this, Top20.class);
startActivity(i);
finish();
}
});
Run Code Online (Sandbox Code Playgroud)
在所有这些修正之后它仍然是相同的:S当我像疯子一样点击时,多个活动在历史堆栈上,我必须多次退出.
有什么建议 ?什么mi做错了?
我想在Button视图中删除文本周围的填充.第一个截图是我将实现的结果,第二个截图是最先进的.


当然,我已经定义了一个自定义drawable来获得按钮外观.但即使我将padding属性设置为0dp,结果也不会改变.
请问有什么建议吗?
编辑 这是按钮的xml代码
<Button
android:id="@+id/btnCancel"
style="@style/dark_header_button"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:padding="0dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@android:string/cancel" />
Run Code Online (Sandbox Code Playgroud)
这是样式xml文件:
<style name="dark_header_button">
<item name="android:background">@drawable/bkg_dark_header_button</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowRadius">1</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是可绘制的xml文件:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<corners android:radius="10dp" />
<gradient
android:angle="90"
android:endColor="#060606"
android:startColor="#707070"
android:type="linear" />
</shape>
</item>
<item>
<shape>
<corners android:radius="10dp" />
<gradient
android:angle="90"
android:endColor="#707070"
android:startColor="#060606"
android:type="linear" />
<stroke
android:width="0.5dp"
android:color="#2b2b2b" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
我试图在这里创建一个类似于圆形按钮的按钮 -
http://livetools.uiparade.com/index.html
(每个按钮看起来都在沉浸的部分内)我通过放置按钮
进入它一个圆圈背景,
虽然我得到了这个结果,但给它们两个渐变的结果并没有结果相同-
(我会尽可能上传我的代码)我怎样才能达到同样的效果?
无法将 MaterialToggleButton 的选定颜色设置为纯色,仅显示原色的透明阴影。
我尝试了下面的代码集,输出图像如下所示。
样式中的按钮主题
<style name="ThemeButtonColorToggle" parent="AppTheme">
<item name="colorPrimary">@color/colorOrange</item>
<item name="colorButtonNormal">@color/colorOrange</item>
<item name="android:backgroundTint">@color/black</item>
<item name="strokeColor">@color/colorOrange</item>
<item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
<item name="colorOnPrimary">@color/colorOrange</item>
<item name="colorOnPrimarySurface">@color/colorOrange</item>
</style>
Run Code Online (Sandbox Code Playgroud)
XML代码
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedButton="@id/btnAndroid"
app:layout_constraintTop_toBottomOf="@id/tvName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:theme="@style/ThemeButtonColorToggle"
android:textColor="@android:color/white"
android:textColorHighlight="@color/colorOrange"
android:text="Android" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btniOS"
android:layout_width="wrap_content"
android:textIsSelectable="true"
android:textColor="@android:color/white"
android:theme="@style/ThemeButtonColorToggle"
android:layout_height="wrap_content"
android:text="iOS" />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
我需要得到如下所示的纯色
谁能帮我解决一下
android android-button android-togglebutton material-components-android
我想在 Jetpack Compose 中实现MaterialButtonToggleGroup。该组件如下所示(图片取自此处):

到目前为止,我得到了以下结果:

请注意,存在垂直蓝色边框旁边的垂直灰色边框。在原作中,要么同时出现彩色边框,要么同时出现灰色。为了看得更清楚,看一下这张带有超厚边框的图像:

如何实现两个按钮之间不存在垂直边框?我当前的代码如下所示:
val cornerRadius = 8.dp
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Spacer(modifier = Modifier.weight(1f))
items.forEachIndexed { index, item ->
OutlinedButton(
onClick = { indexChanged(index) },
shape = when (index) {
// left outer button
0 -> RoundedCornerShape(topStart = cornerRadius, topEnd = 0.dp, bottomStart = cornerRadius, bottomEnd = 0.dp)
// right outer button
items.size - 1 -> RoundedCornerShape(topStart = 0.dp, topEnd = cornerRadius, bottomStart = 0.dp, bottomEnd = cornerRadius) …Run Code Online (Sandbox Code Playgroud) android modifier android-button kotlin android-jetpack-compose
我正在为人工智能学期项目编写Android游戏应用程序.我在主类的第38行得到一个NullPointerException,它是启动屏幕上新游戏按钮的setOnClickListener.
相关章节类WW3活动:
public class WW3Activity extends Activity
{
public boolean DebugMode = false;
private String buildMenuEmplacement = "";
//[Initialization] Called when the app is first launched
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.startscreen);
//[Initialization] Makes the button resources available to the class
final Button newGame = (Button) findViewById(R.id.buttonNewGame);
final Button loadGame = (Button) findViewById(R.id.buttonLoadGame);
final Button exitGame = (Button) findViewById(R.id.buttonExit);
final Button about = (Button) findViewById(R.id.buttonAbout);
final Button troll = (Button) findViewById(R.id.buttonTroll);
final Button debug = (Button) findViewById(R.id.buttonDebug);
//[Action] …Run Code Online (Sandbox Code Playgroud) 
我正在尝试创建一个看起来如上图所示的按钮.最初我的想法是创建一个9补丁并将其设置为按钮背景.但是,因为这是一个简单的按钮,我想我们可以在不使用任何图像的情况下以某种方式绘制它.
按钮背景颜色为#0c0c0c边框颜色为#1a1a1a文本颜色为#cccccc
我在SO上发现了一个类似的问题,但它创建了一个渐变 -
Android - 按钮的边框
我试着理解Button的默认背景(android 26).
sdk/platforms/android-26/data/res/drawable/btn_default_material.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>
Run Code Online (Sandbox Code Playgroud)
引用:
sdk/platforms/android-26/data/res/drawable/btn_default_mtrl_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/button_inset_horizontal_material"
android:insetTop="@dimen/button_inset_vertical_material"
android:insetRight="@dimen/button_inset_horizontal_material"
android:insetBottom="@dimen/button_inset_vertical_material">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="@color/btn_colored_background_material">
<corners android:radius="@dimen/control_corner_material" />
<solid android:color="@color/white" />
<padding android:left="@dimen/button_padding_horizontal_material"
android:top="@dimen/button_padding_vertical_material"
android:right="@dimen/button_padding_horizontal_material"
android:bottom="@dimen/button_padding_vertical_material" />
</shape>
</item>
</ripple>
</inset>
Run Code Online (Sandbox Code Playgroud)
我理解插图.这是按钮背景图像周围的空间.我不明白填充.背景图像中没有文字.
提出一个思考方法:如果删除padding标签会发生什么?
你可能会得出结论,没有任何事情会发生(我不确定,什么都不会发生).但如果没有任何反应,你有一个问题是什么对它有用?他们为什么把它放在那里?
我有这个问题,我不知道它来自哪里,我已经用自定义背景创建了这个按钮,但是背景颜色是原色,除非更改原色,否则无法更改它。
<Button
android:id="@+id/btn_teacher"
style="@style/normalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_16sdp"
android:background="@drawable/btn_rounded_stroke"
android:text="@string/txt_teacher" />
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="bg_color">#FFD7A4</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
我有很多不同颜色的按钮,所以我不能改变原色
这是我的可绘制背景
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<size android:height="@dimen/_48sdp" />
<corners android:radius="@dimen/_24sdp" />
<stroke android:width="@dimen/_1sdp" android:color="#59CECE" />
<solid android:color="@android:color/white"/>
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我正在使用谷歌的新材料设计
implementation "com.google.android.material:material:1.3.0-alpha02"
Run Code Online (Sandbox Code Playgroud)
我怎样才能覆盖这种颜色?

xml android button android-button material-components-android
android ×10
android-button ×10
material-components-android ×2
android-ui ×1
android-view ×1
button ×1
kotlin ×1
modifier ×1
xml ×1