android - Android 屏幕的新按钮

标签 android button

如何在屏幕上显示按钮?我把它定义为

final Button nappi = (Button) findViewById(R.id.soita);

<Button 
  android:layout_width="100px"
  android:layout_height="wrap_content"
  android:text="@+string/Soita"
  android:id="@+id/soita" 
/>

最佳答案

您需要创建要在其中显示按钮的 View 的布局。甚至像

假设您将定义存储在 res/layout 目录中名为 main.xml 的文件中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

然后在您的 Activity 中,您需要执行以下操作:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Replace this with the name of your xml file dictating layout
        setContentView(R.layout.main);
    }
}

请参阅 Hello Views 教程;很有用。 http://developer.android.com/guide/tutorials/views/index.html

如果您问的是不同的问题,例如以编程方式向现有 View 添加按钮,我不确定。在这种情况下,您可能希望按钮一开始是隐藏的,然后在需要时显示出来。

关于android - Android 屏幕的新按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498046/

相关文章:

javascript - 当我单击按钮时,将变量作为文本放在按钮上

java - 将 HTTPOnly cookie 与 android 中的其他 cookie 区分开来

android - 查找 Listview 项目的像素位置似乎返回错误值

html - 按钮没有在图像上方并排显示

java - 无法将按钮文本设置为 "<<<"

Android:如何让Borderless button的文字大小和TextView一样

android - 如何找到挂载到android的USB路径?

java - 最小化 setOnSeekBarChangeListener 样板

javascript - 值不能从 ReadableNativeMap 转换为 double

android - 如何创建标准的无边框按钮(如提到的设计指南中)?