android - 是否可以在没有父 View 的情况下设置 wrap_Content 属性?

标签 android view android-wrap-content

我有一个简单的按钮 View ,并将其设置为内容 View 。是否可以通过编程方式将此 View 的大小调整为“wrap_content”?

Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);

最佳答案

使用以下代码设置您的属性:

Button button = new Button(getContext());

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);

button.setText("Some text");
setContentView(button);

关于android - 是否可以在没有父 View 的情况下设置 wrap_Content 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843527/

相关文章:

android - URL 提取完成时禁用进度条 : A standard scenario

SQL:创建一个基于日期创建位置的 View

ajax - 从 Asp.net View 页面返回带有 Ajax 调用的 View 的调用 Controller 方法

c# - ServiceStack: View 中未使用自定义应用程序设置

android - GridLayoutManager - 列宽包裹自己最大的 child

java - 查询所有联系人的搜索

android - 是否有 Android 广播在设备密码更改时发出通知?

android - Google Nearby 通知 API 不显示新通知

android - 如何在 Fresco 库中将 DraweeView 的尺寸设置为 "wrap_content"

Android Horizo​​ntal RecyclerView wrap_content