标签: android-button

使用XML布局作为模板以编程方式创建android按钮

我有一个包含TextView的LinearLayout,并且总是会.TextView下面始终至少有一个按钮,但在某些情况下可能会有多个按钮.

我可以通过编程方式成功创建和添加任意数量的按钮.我还可以通过编程方式成功设置这些按钮所需的任何外观相关参数/选项.

问题是我不知道如何告诉以编程方式创建的按钮它应该使用包含外观和布局参数的XML资源文件,而不是以编程方式设置这些参数.

我看过类似命名的问题并花时间搞乱API本身,但无济于事.

编辑:
这是我正在尝试做的一个近似值,希望能让我的解释更加清晰:

private TextView textView;
private SomeObject someObject;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    View scrollView = inflater.inflate(R.layout.fragment_play_game, container, false);
    textView = (TextView) scrollView.findViewById(R.id.game_data_text);
    textView.setText(someObject.getTextForTextView());

    LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.game_data_container);
    for (String optionText : someObject.getTextForButtons()) {
        layout.addView(createOptionButton(optionText, layout));
    }
    return scrollView;
}

private View createOptionButton(String optionText, LinearLayout layout) {
    Button optionButton = new Button(this.getActivity());
    // set button layout/options here, somehow??
    optionButton.setText(optionText);
    return optionButton;
}
Run Code Online (Sandbox Code Playgroud)

片段的我的XML布局文件看起来像这样(这是我试图添加按钮的LinearLayout):

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-xml android-button

7
推荐指数
1
解决办法
6988
查看次数

如何在按钮上显示动态文本并在Android中自动调整其大小?

我正在开发一个项目,该项目需要根据用户在按钮上的选择显示一些动态文本。

我知道如何显示文本,但是我对如何在固定大小的按钮上正确显示其他文本感到困惑。

例如:“苹果”和“我有一个苹果”。我如何获得这样的结果:当显示“ Apple”时,文本大小会变大并适合按钮;当显示“我有Apple”时,文本会变小并可能变成三行?

谢谢!

android android-layout android-button

7
推荐指数
2
解决办法
2万
查看次数

如何在每个项目的列表视图中实现简单的按钮

在此输入图像描述

我的列表视图项中有一些条目.在那里,我有一个简单的"喜欢按钮"(不像Facebook按钮).你可以看到上面提到的SCREENSHOT; 供参考.我点击按钮的那一刻; 当我再次登录时,我希望更改类似按钮的颜色,并且类似按钮的颜色应该保持不变(更改类似).

此外,所有条目必须使用json,使用cust_id,bus_id,Offer_id填充数据库; 我非常清楚.

当我再次点击相同的按钮(如按钮)时,其颜色已被更改.必须将其更改回默认颜色,并且必须从数据库中删除数据.

我怎样才能做到这一点...?1.如何获得点击按钮的价值.2.如何将更改的颜色恢复为默认值; 一旦按钮被重新点击.

Plz建议我......

这是按钮代码

holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clicked) {
                    holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
                } else {
                    holder.b1.setBackgroundResource(R.drawable.like_icon);
                }
                clicked = true;
            }
        });
Run Code Online (Sandbox Code Playgroud)

android listview android-intent android-button

7
推荐指数
1
解决办法
5714
查看次数

制作带边框的圆形按钮

我正在尝试制作一个带有透明背景和彩色边框的圆形按钮.我怎样才能做到这一点?

我附上了我的一个iOS应用程序的屏幕截图,显示了我想要的内容.

在此输入图像描述

android android-button

7
推荐指数
2
解决办法
2万
查看次数

在按钮内设置可绘制位置

我在文本的左边有一个按钮和一个drawable,但我希望drawable更接近文本.所以我需要移动drawable.

我已经定义了android:drawableLeft但是按钮的内容没有居中.

这是我的代码:

<Button
    android:id="@+id/addsubject"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_action_add"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:text="@string/addsubject"
    android:textColor="@color/white"
    android:background="@drawable/custombutton1" />
Run Code Online (Sandbox Code Playgroud)

以下是它现在的样子:

在此输入图像描述

以下是我喜欢的方式:

在此输入图像描述

谢谢!

android android-button android-drawable

7
推荐指数
3
解决办法
2万
查看次数

带有5.0 FAB阴影的圆形按钮

我想要一个带阴影的圆形按钮.暗影必须像FAB一样.目前我正在使用png来实现按钮.

http://i62.tinypic.com/vh6fzb.jpg

但是当我使用这个图像时,我无法给出正确的按下状态变化.有没有办法通过使用xml来实现这一点

android android-xml android-button material-design

7
推荐指数
2
解决办法
7889
查看次数

创建和定位浮动操作按钮

我正在尝试将FAB添加到卡片视图的底部,就像在此应用中一样: 应用图片

该按钮确实显示,但它不与卡片视图重叠.我正在使用Android Material Design Library.这是我的文件:

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FFEB3B</color>
    <color name="colorAccentDark">#FBC02D</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

activity_main.xml:

    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <LinearLayout
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/main_content"
        android:layout_below="@id/toolbar">

        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/stop_card"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginTop="5dp"
            card_view:cardCornerRadius="4dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="5dp"
                android:paddingRight="10dp"
                android:paddingBottom="5dp">

                <TextView
                    android:id="@+id/label_stop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Fermata"
                    android:textSize="25sp"
                    android:textStyle="bold"
                    android:singleLine="true" />

                <EditText
                    android:id="@+id/edit_stop"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:ems="10"
                    android:layout_gravity="end" />
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="72dp" >

            <com.gc.materialdesign.views.ButtonFloat
                android:id="@+id/buttonFloat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#1E88E5" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-button material-design

7
推荐指数
1
解决办法
6997
查看次数

删除边界和按钮边框之间的空间

如何删除Button边框和视图边界之间的空间尝试设置padding0dp但没有结果。
这些是具有默认样式的材质按钮。

在此处输入图片说明 在此处输入图片说明

android android-button material-design material-components material-components-android

7
推荐指数
1
解决办法
1017
查看次数

Android Jetpack 撰写 IconButton 填充

如何删除 IconButton 中的填充?我希望我的列中的项目具有相同的开始填充

预览图

Column(
    modifier = Modifier
        .fillMaxWidth() 
        .padding(horizontal = 16.dp)
) {
    IconButton(onClick = { }) {
        Icon(asset = Icons.Filled.Search)
    }
    Text("Some text")
} 
Run Code Online (Sandbox Code Playgroud)

android android-button android-jetpack-compose

7
推荐指数
2
解决办法
4171
查看次数

如何使用jetpack compose创建圆形轮廓按钮

我正在尝试创建一个圆形OutlinedButton,中间有一个图标,没有文字。

    OutlinedButton(onClick = { /*TODO*/ },
        shape = CircleShape,
        border= BorderStroke(1.dp, Color.Blue)
    ) {
        Icon(Icons.Default.Add, contentDescription = "content description")
    }
Run Code Online (Sandbox Code Playgroud)

最后一个按钮是椭圆形的:

在此处输入图片说明

使用IconButton

    IconButton(onClick = {  },
        modifier = Modifier
            .clip(CircleShape)
            .border(1.dp, Color.Red)
    ) {
        Icon(Icons.Default.Add, contentDescription = "content description",tint = Color.Red)
    }
Run Code Online (Sandbox Code Playgroud)

这是最终结果:

在此处输入图片说明

android android-button android-jetpack-compose android-compose-button

7
推荐指数
1
解决办法
1715
查看次数