如何在彼此下方设置2个EditText视图?我究竟做错了什么?

时间:2017-05-10 13:54:54

标签: android android-relativelayout

我是初学者,不深入了解android包

package com.example.shiben.javaproject;

import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.graphics.Color;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //creating relativelayout
        RelativeLayout app_layout = new RelativeLayout(this);

        //Objects for layout
        Button login_button = new Button(this);
        EditText uname = new EditText(this);
        ImageView logo = new ImageView(this);
        logo.setImageDrawable(getDrawable(R.drawable.oil));
        EditText pwd = new EditText(this);


        RelativeLayout.LayoutParams login_button_position = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams uname_position = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams pwd_position = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams logo_position = new RelativeLayout.LayoutParams
                (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        //adding objects to layout
        app_layout.addView(login_button, login_button_position);
        app_layout.addView(uname, uname_position);
        app_layout.addView(logo, logo_position);
        app_layout.addView(pwd, pwd_position);

        //standard line of code to convert dp to pixels for objects
        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300,
                r.getDisplayMetrics());

        //position and style
        uname_position.addRule(RelativeLayout.ABOVE, login_button.getId());
        pwd_position.addRule(RelativeLayout.ABOVE, uname.getId());
        uname_position.addRule(RelativeLayout.CENTER_HORIZONTAL);
        uname_position.setMargins(0,0,0,50);
        pwd_position.addRule(RelativeLayout.CENTER_HORIZONTAL);
        pwd_position.setMargins(0,0,20,50);
        login_button.setText("Login");
        login_button.setBackgroundColor(Color.GREEN);
        login_button_position.addRule(RelativeLayout.CENTER_HORIZONTAL);
        login_button_position.addRule(RelativeLayout.CENTER_VERTICAL);
        logo_position.addRule(RelativeLayout.ALIGN_BOTTOM);
        logo_position.setMargins(0,0,0,50);
        pwd.setWidth(px);
        uname.setWidth(px);



        //assigning id to objects
        login_button.setId(1);
        uname.setId(2);
        pwd.setId(3);

        //app start page
        setContentView(app_layout);

            }
        }
    }
}

0 个答案:

没有答案