我正在尝试创建一个应用程序,当单击弹出窗口中的按钮时,将在同一活动的另一个布局中创建一个新按钮。
以下是popupwindow.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start_goal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_goal" />
</RelativeLayout>
以下是activitymain.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eugene.trackeruptest3.MainActivity"
android:background="@color/darkgrey"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="355dp">
<Button
android:id="@+id/setupgoal"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:text="@string/setup_goal"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/buttonContainer">
</RelativeLayout>
</LinearLayout>
这是popupwindow
的java代码public class creategoal extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.popupwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
}
}
这是MainActivity.java的java代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button setupgoal = (Button) findViewById(R.id.setupgoal);
setupgoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,creategoal.class));
}
});
}
}
答案 0 :(得分:1)
您是否尝试在XML上使用visibility:gone
并在onClick
方法上使用view.setVisibility(Visibility.VISIBLE);
进行设置?