如何设置弹出窗口的屏幕自动适合水平屏幕的长度

时间:2017-07-20 18:37:30

标签: java android xml android-layout

我希望我的Pop up windown适合水平长度中移动屏幕的每个大小?这里是我的Java和XML代码:So On Button点击菜单2活动中出现的弹出窗口,我希望它与屏幕完全一致,垂直方向应该有1000的长度。

JAVA:

package amapps.impossiblequiz;

public class Menu2 extends AppCompatActivity {

    private DrawerLayout mDrawerLayout2;
    private ActionBarDrawerToggle mToggle;
    private Toolbar mToolbar;
    private Button popup;
    private PopupWindow popupWindow;
    private LayoutInflater layoutInflater; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu2);

        TextView txtScore = (TextView) findViewById(R.id.textScore2);
        TextView txtHighScore = (TextView)findViewById(R.id.textHighScore);
        ImageView imgTrophyView1 = (ImageView)findViewById(R.id.trophy1);
        ImageView imgTrophyView2 = (ImageView) findViewById(R.id.trophy2);
        TextView trophy1Txt = (TextView) findViewById(R.id.trophy1Text);
        TextView trophy2Txt = (TextView) findViewById(R.id.trophy2Text);

        Intent intent = getIntent();
        int mScore = intent.getIntExtra ("score",0);
        txtScore.setText("Your score is: " + mScore);

        SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
        int applyView =sharedpreferences.getInt("currentscore",0);

        SharedPreferences mypref =getPreferences(MODE_PRIVATE);
        int highScore = mypref.getInt("highScore", 0);
        if (highScore>= mScore)
            txtHighScore.setText("High score: " + highScore);

        else{
            txtHighScore.setText("New highscore: " + mScore);

            SharedPreferences.Editor editor = mypref.edit();
            editor.putInt("highScore",mScore);
            editor.commit();
        }

        if (applyView >=10) {
            imgTrophyView1.setVisibility(View.VISIBLE);
            trophy1Txt.setVisibility(View.VISIBLE);
        }
            if (applyView >= 20){
                imgTrophyView2.setVisibility(View.VISIBLE);
                trophy2Txt.setVisibility(View.VISIBLE);
        }

        popup = (Button)findViewById(R.id.enablePOPUP);
        popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutInflater =(LayoutInflater) 
              getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                ViewGroup container = 
                (ViewGroup)layoutInflater.inflate(R.layout.popup_menu2_1,null);
                popupWindow = new PopupWindow(container,1000,980,true); 
                popupWindow.showAtLocation(mDrawerLayout2, 
                Gravity.NO_GRAVITY,500,500);

                container.setOnTouchListener(new View.OnTouchListener(){
                    @Override

                            public boolean onTouch(View view, MotionEvent 
              motionEvent){
                            popupWindow.dismiss();
                            return true;
                    }
                });
            }
        });

        mToolbar = (Toolbar)findViewById(R.id.nav_action);
        setSupportActionBar(mToolbar);
        mDrawerLayout2 = (DrawerLayout) findViewById(R.id.drawerLayout2);

        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout2, R.string.open, R.string.close);
        mDrawerLayout2.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        NavigationView mNavigationView = (NavigationView) findViewById(nv2);
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem){
                switch (menuItem.getItemId()){
                    case(R.id.nav_home2):
                        Intent accountActivity2 = new Intent(getApplicationContext(),QuizActivity.class);
                        startActivity(accountActivity2);

                }
                return true;
            }
        });}

        public void onClick(View view) {

            Intent intent = new Intent(Menu2.this, QuizActivity.class);
            startActivity(intent);
    }

    @Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

XML:

<LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:background="#000"
      android:alpha="0.95">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:text="You reached a score of 10, you have a basic 
        knowledge!"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Button"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/trophy1"
        android:layout_marginTop="22dp"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可以在我的调用功能下获得设备的全宽

private int getDeviceWidth(Context context) {
     WindowManager windowManager = (WindowManager) 
       context.getSystemService(Context.WINDOW_SERVICE);
     int screenWidth = windowManager.getDefaultDisplay().getWidth();
     return screenWidth;
}

并将值传递给 PopupWindow 构造函数。

答案 1 :(得分:0)

使用构造函数PopupWindow (View contentView, int width, int height, boolean focusable)时,请尝试将参数width替换为LinearLayout.LayoutParams.MATCH_PARENT。试一试。