Android屏幕的新按钮

时间:2009-09-30 13:15:24

标签: 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" 
/>

3 个答案:

答案 0 :(得分:4)

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

这样的东西

假设您将定义存储在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>

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

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

如果你问的是不同的东西,比如以编程方式向现有视图添加按钮,我不确定。在这种情况下,您可能希望按钮开始隐藏,然后在需要时显示它。

答案 1 :(得分:0)

我不明白为什么没有显示任何内容。我试着创建自己的简单用户界面。

主:

package com.xyz;
import android.app.Activity;
import android.os.Bundle;

public class NewHomeScreen extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.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"
>
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:text="@string/hello" 
>
</TextView>
<Button android:text="Soita"
    android:id="@+id/soita" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
>
</Button>
</LinearLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name="NewHomeScreen">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.HOME"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>    
  </activity>
</application>
<uses-sdk android:minSdkVersion="3" />

</manifest> 

答案 2 :(得分:0)

您的textview获取所有屏幕位置... 试试这个

XML:

<TextView  
    android:text="@string/hello" 
    android:layout_width="match_parent" android:layout_height="291dp">
</TextView>