我正在尝试创建自定义AlertDialog,并且布局中有两个按钮.但是,当按钮布局太长时,它只显示第一个单词.我已经设置了xml singleLine = "false",但没有.
此外,我在代码中设置了按钮的行数,在本例中为2.但是当文本不长时,按钮保持很大,而不是包装内容.
我想做的是让按钮包裹整个文本,即使它打破按钮的文本行太长,如果左按钮因为包装整个文本而得到更大的高度,按钮在它的侧面应该是相同的高度.我知道Android会自动执行此操作,但我不知道为什么它不会发生.
在下面的图像中,按钮中的文本应为"这是一个测试按钮",但它会剪切文本,并且不会修改内容而不修改其宽度.
以下是它的外观截图:

这是这个布局的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="280dip" >
<LinearLayout
android:id="@+id/dialogTitleLayout"
android:orientation="vertical"
android:background="@drawable/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
style="@style/DialogText.Title"
android:id="@+id/dialogTitle"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/dialogContentLayout"
android:orientation="vertical"
android:background="@drawable/center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
style="@style/DialogText"
android:id="@+id/dialogContent"
android:layout_width="270dip"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp" />
<LinearLayout
android:id="@+id/buttonsLayout"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dp" >
<Button
android:id="@+id/neutralButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/button"
android:textColor="#FFFFFF"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="90dp"
android:layout_marginLeft="90dp"
android:layout_marginBottom="10dp"
android:singleLine="false" />
<Button
android:id="@+id/positiveButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:textColor="#FFFFFF" …Run Code Online (Sandbox Code Playgroud) 我希望在Android操作系统上点击按钮时实现倾斜效果.
倾斜效果:不会将整个按钮视为按下.应该只触摸触摸事件发生的部分.
这在Android上很容易实现吗?

我想将按钮映射到一个按钮数组,并且代码在编译时没有错误但是当我运行它时有强制关闭:
Button buttons[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_board_view);
// Set OnClick listeners
Button buttons[] = null;
buttons[0] = (Button)findViewById(R.id.buttonOne);
buttons[1] = (Button)findViewById(R.id.buttonTwo);
buttons[2] = (Button)findViewById(R.id.buttonThree);
buttons[3] = (Button)findViewById(R.id.buttonFour);
buttons[4] = (Button)findViewById(R.id.buttonFive);
buttons[5] = (Button)findViewById(R.id.buttonSix);
buttons[6] = (Button)findViewById(R.id.buttonSeven);
buttons[7] = (Button)findViewById(R.id.buttonEight);
buttons[8] = (Button)findViewById(R.id.buttonMid);
}
Run Code Online (Sandbox Code Playgroud)
logcat的:
03-26 21:42:51.455: D/dalvikvm(1156): GC_EXTERNAL_ALLOC freed 55K, 53% free 2566K/5379K, external 1625K/2137K, paused 98ms
03-26 21:42:54.323: D/AndroidRuntime(1156): Shutting down VM
03-26 21:42:54.323: W/dalvikvm(1156): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-26 21:42:54.343: E/AndroidRuntime(1156): …Run Code Online (Sandbox Code Playgroud) 我想通过使用XML定义使用按钮(TextView)创建它:

在我的活动布局中:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_arrow" <!-- I NEED IMPLEMENT THIS -->
android:clickable="true"
android:drawablePadding="7dp"
android:gravity="center"
android:drawableLeft="@drawable/music_cloud"
android:onClick="exportSong"
android:padding="20dp"
android:text="@string/export_upload"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/dark_yellow_text_color"
android:textStyle="bold" />
Run Code Online (Sandbox Code Playgroud)
我创建了几个帖子:
我尝试修改几个XML定义,但没有什么是好的.如何实现这种形状有一些简单的方法吗?它也应该有一个透明的背景.
材料规格显示禁用的按钮状态,显示为灰色。
https://www.material.io/design/components/buttons.html#toggle-button
我正在从Android的材质组件中使用MaterialButton:https: //www.material.io/develop/android/components/material-button/
但是,将按钮设置为禁用时,按钮的颜色/色调不会改变。
<com.google.android.material.button.MaterialButton
android:id="@+id/disabled_material_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/button_label_disabled"/>
Run Code Online (Sandbox Code Playgroud)
只是默认情况下未在Material Android组件中实现?材料组件是否定义了禁用的按钮状态列表?
android colors android-layout android-button material-design
是否有选项可以MaterialButtonToggleGroup在使用时选择所需的按钮app:singleSelection="true"?
当单击不同的按钮工作正常(所有其他按钮都被取消选择),但是当您单击同一个(已选择)按钮时,它会自行取消选择它,我想保持选中状态。
我的例子:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap"
android:layout_height="wrap_content"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/filterA"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/filterB"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/filterC"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud) android android-button android-togglebutton material-components material-components-android
根据文档
支持旧版本平台上兼容功能的按钮,包括:
允许通过 ViewCompat 中的背景色调方法对其背景进行动态色调。允许使用 R.attr.backgroundTint 和 R.attr.backgroundTintMode 设置背景色调。当您在布局中使用 Button 并且顶级活动/对话框由 appcompat 提供时,这将自动使用。您应该只需要在编写自定义视图时手动使用此类。
现在,这让我假设以下两个按钮在高级设备上看起来完全相同。
<androidx.appcompat.widget.AppCompatButton
android:text="AppCompatButton"
android:id="@+id/appcompatbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_below="@id/appcompatbutton"
android:id="@+id/button"
android:layout_width="wrap_content"
android:text="Button"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
然而,这是它的实际外观:
我在以下模拟器上运行了这个:Galaxy Nexus,API:28 (720 x 1280 xhdpi)
当我像这样在 appTheme 中应用 buttonStyle 时:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="buttonStyle">@style/Widget.MaterialComponents.Button</item>
</style>
Run Code Online (Sandbox Code Playgroud)
它改变了 AppCompatButton 而不是像这样的普通按钮:(注意圆角边缘的细微差别)
我还尝试创建一个既继承自 android.widget.Button 又继承自 androidx.appcompat.widget.AppCompatButton 的自定义按钮,这两个按钮显示的行为与在 xml 中使用 AppCompatButton 的行为相同。所以感觉唯一的异常值是 XML 中的 Button。
问题 1: …
android android-button material-design material-components-android androidx
执行以下操作后,我的按钮保持在突出显示状态时出现问题:
public class MainActivity extends AppCompatActivity {
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton button = (AppCompatButton) findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Test", "calling onClick");
}
});
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP: {
v.getBackground().clearColorFilter();
v.invalidate();
v.performClick();
Log.d("Test", "Performing click");
return true;
}
}
return false;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
关于上面的代码,在使用它时,我希望按钮单击由触摸来处理,并且通过返回“ true”,处理应在touchListener处停止。
但这种情况并非如此。即使正在调用单击,按钮仍保持突出显示状态。
我得到的是:
Test - …Run Code Online (Sandbox Code Playgroud) Button(backgroundColor = Color.Yellow) {
Row {
Image(asset = image)
Spacer(4.dp)
Text("Button")
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我不能在 Button 上使用背景颜色我在 Compose Layout codelabs 上遵循了 Image() 中的 backgroundColor 和资产问题。请帮我弄清楚如何使用Buton,我还是新手
android android-button android-jetpack-compose android-compose-button
我试图让两个按钮在它们周围发出微弱的光,有点脉动褪色效果

按钮有一个背景可绘制图像,我只想在它周围有一个发光的效果,让它与播放的音乐产生共鸣
我已经搜索了多个线程,它们要么只是一个图像视图,要么是按下的,要么是位图,所以不一定是我正在寻找的
这是我的按钮 XML:
<Button
android:id="@+id/playEasyMode_Button"
android:layout_width="145dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/play_easy_button"
android:onClick="playEasyMode"
android:layout_margin="15dp"/>
Run Code Online (Sandbox Code Playgroud)
请不要尝试建议在按下时使用背景可绘制对象等等,这不是我想要做的。我想以编程方式或在 XML 中设置这种效果
编辑:Object Animator offloat 相对布局错误图像 edit2:好的,因为我几乎达到了我想要的效果,下面是 Aman Verma,这是我现在更新的 XML:
<RelativeLayout
android:id="@+id/relative_layout_id"
android:layout_width="160dp"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="#FFC107"
android:alpha=".1"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp">
<Button
android:id="@+id/playEasyMode_Button"
android:layout_width="145dp"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:background="@drawable/play_easy_button"
android:onClick="playEasyMode"
android:layout_marginTop="8dp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen_layout);
//glowing button effect
RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(relativelayout, "alpha", .5f, .1f);
fadeOut.setDuration(300);
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(relativelayout, "alpha", .1f, .5f);
fadeIn.setDuration(300);
final AnimatorSet mAnimationSet = new …Run Code Online (Sandbox Code Playgroud) android ×10
android-button ×10
material-components-android ×2
androidx ×1
arrays ×1
colors ×1
textview ×1
xml ×1