位置布局视图到顶部

时间:2014-08-04 20:04:52

标签: java android android-layout android-activity

我创建了一个个人资料创建页面,用户可以在其中回答各种问题。我的问题是,当加载应用程序时,布局不会从顶部显示。从某种意义上说,你必须向上滚动才能看到顶部的字段。我认为问题可能会在编辑文本标题(etxheadline)周围解决,当活动加载时,它似乎集中在它上面。

提前致谢

以下是布局代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dark_texture_blue" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="797dp"
    android:gravity="center"
    android:orientation="vertical" >

    <com.mikhaellopez.circularimageview.CircularImageView
        android:id="@+id/profilePicturePreview"
        android:layout_width="132dp"
        android:layout_height="120dp"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:alpha="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:layout_above="@+id/etxtage"
        android:layout_alignLeft="@+id/etxtname"
        android:alpha="0.8"
        android:background="#330099"
        android:text="Upload from Facebook"
        android:textColor="#ffffff"
        android:textSize="17sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnPictureSelect"
        android:layout_width="118dp"
        android:layout_height="60dp"
        android:layout_alignRight="@+id/etxtname"
        android:layout_below="@+id/profilePicturePreview"
        android:layout_marginRight="8dp"
        android:alpha="0.8"
        android:background="#000000"
        android:onClick="pickPhoto"
        android:text="Select photo from gallery"
        android:textColor="#ffffff"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="9dp"
        android:text="Preferred Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ADD8E6"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:gravity="center"
        android:text="Profile Creation"
        android:textColor="#ffffff"
        android:textSize="28sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Age"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ADD8E6"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <EditText
        android:id="@+id/etxtage"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnPictureSelect"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:ems="10"
        android:hint="Please type your age here"
        android:inputType="number"
        android:maxLength="2"
        android:textAlignment="center"
        android:textColor="#f2f2f2"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/etxtname"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView6"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:enabled="true"
        android:hint="Please type your name here"
        android:inputType="textPersonName"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etxtname"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="Upload your Profile Picture"
        android:textColor="#f2f2f2"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toLeftOf="@+id/textView3" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textColor="#f2f2f2"
            android:text="Male" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#f2f2f2"
            android:text="Female" />

    </RadioGroup>

     <SeekBar
        android:id="@+id/seekBarDistance"
        android:layout_width="250dp"
        android:progress="50"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView12"
        android:layout_marginTop="11dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/etxtheadline"
        android:layout_marginTop="38dp"
        android:text="I am a"
        android:textColor="#ADD8E6"
        android:textSize="20sp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/radioGroup3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignTop="@+id/radioGroup2"
        android:layout_marginTop="10dp" >
    </RadioGroup>

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etxtheadline"
        android:layout_below="@+id/textView2" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male"
            android:textColor="#f2f2f2" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:textColor="#f2f2f2" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignRight="@+id/radioGroup2"
        android:text="Looking for"
        android:textColor="#ADD8E6"
        android:textSize="20sp"
        android:textStyle="bold" />

    <SeekBar
        android:id="@+id/seekBarMinimumAge"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView7"
        android:layout_marginTop="11dp"
        android:progress="25" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etxtage"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Minimum Age Looking For"
        android:textColor="#f2f2f2"
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <EditText
        android:id="@+id/etxtheadline"
        android:layout_width="270dp"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/textView8"
        android:hint="A quick description of yourself"
        android:singleLine="true"
        android:textAlignment="center"
        android:textColor="#f2f2f2"
        android:textSize="18dp" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/seekBarDistanceValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/tMinAge"
        android:layout_below="@+id/seekBarDistance"
        android:text="50"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#f2f2f2"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="Search Distance (100KM)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ADD8E6"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/tMinAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBarMinimumAge"
        android:layout_centerHorizontal="true"
        android:text="25"
        android:textColor="#f2f2f2"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tMaxAge"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Headline"
        android:textColor="#ADD8E6"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tMinAge"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Maximum Age Looking For"
        android:textColor="#f2f2f2"
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="serif" />

     <SeekBar
        android:id="@+id/seekBarMaximumAge"
        android:layout_width="221dp"
        android:progress="50"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView14"
        android:layout_marginTop="11dp" />

        <TextView
            android:id="@+id/tMaxAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/seekBarMaximumAge"
            android:layout_centerHorizontal="true"
            android:text="50"
            android:textColor="#f2f2f2"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView16"
            android:layout_centerHorizontal="true"
            android:text="I agree to the terms and Conditions"
            android:textColor="#D2D2D2"
            android:textColorHint="#ffffff" />

         <TextView
             android:id="@+id/textView16"
             android:layout_width="280dp"
             android:layout_height="40dp"
             android:layout_alignLeft="@+id/checkBox1"
             android:layout_below="@+id/seekBarDistanceValue"
             android:layout_marginTop="14dp"
             android:gravity="center"
             android:text="Click here to review the terms and conditions"
             android:textColor="#99CCFF"
             android:textSize="16sp" />

         <Button
             android:id="@+id/btnReset"
             android:layout_width="120dp"
             android:layout_height="60dp"
             android:layout_marginBottom="7dp"
             android:layout_below="@+id/checkBox1"
             android:layout_toLeftOf="@+id/btnPictureSelect"
             android:alpha="0.8"
             android:background="#660000"
             android:layout_marginTop="14dp"
             android:text="Reset"
             android:textColor="#ffffff"
             android:textSize="17sp"
             android:textStyle="bold" />

         <Button
             android:id="@+id/btnConfirm"
             android:layout_width="120dp"
             android:layout_height="60dp"
             android:layout_below="@+id/checkBox1"
             android:layout_toRightOf="@+id/seekBarDistanceValue"
             android:alpha="0.8"
             android:layout_marginTop="14dp"
             android:layout_marginBottom="7dp"
             android:background="#330099"
             android:text="Confirm"
             android:textColor="#ffffff"
             android:textSize="17sp"
             android:textStyle="bold" />

</RelativeLayout>

更新 活动代码

 public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;
    protected EditText mName;
    protected EditText mAge;
    protected EditText mHeadline;
    protected Button mConfirm;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);
        ScrollView s = (ScrollView) findViewById(R.id.scrollProfile);
        s.fullScroll(ScrollView.FOCUS_UP);

        Parse.initialize(this, "SBNldSjuNL1F7IOj0OWpwenBWRF7rSkjkm8WFQJj", "DLvq1OqSo87kzEw5j1XNBPKJdXF7dxwRtfEPkxWJ");

        mName = (EditText)findViewById(R.id.etxtname);
        mAge = (EditText)findViewById(R.id.etxtage);
        mHeadline = (EditText)findViewById(R.id.etxtheadline);

        mConfirm = (Button)findViewById(R.id.btnConfirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = mName.getText().toString();
                String age = mAge.getText().toString();
                String headline = mHeadline.getText().toString();


                age = age.trim();
                name = name.trim();
                headline = headline.trim();

                if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                    builder.setMessage(R.string.signup_error_message)
                        .setTitle(R.string.signup_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {
                    // create the new user!
                    setProgressBarIndeterminateVisibility(true);

                    ParseUser currentUser = ParseUser.getCurrentUser(); 
                    currentUser.put("name", name); 
                    currentUser.put("age", age); 
                    currentUser.put("headline", headline); 
                    currentUser.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                // Success!
                                Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                                builder.setMessage(e.getMessage())
                                    .setTitle(R.string.signup_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });


        save = (Button) findViewById(R.id.button2);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
        final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                seekBarValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.seekBarMinimumAge);
        final TextView txtMinimum = (TextView) findViewById(R.id.tMinAge);

        seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMinimum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.seekBarMaximumAge);
        final TextView txtMaximum = (TextView) findViewById(R.id.tMaxAge);

        seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMaximum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this




        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res > 
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");


                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message


            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

2 个答案:

答案 0 :(得分:1)

您需要使用ScrollView实例进行预测。

您需要在ScrollView中添加 id ,并在您的activity / fragment类中的某个位置使用该实例,然后将scrollview向上滚动到顶部

<强>样品:

ScrollView s = (ScrollView) findViewById(R.id.yourid);
s.fullScroll(ScrollView.FOCUS_UP);

答案 1 :(得分:1)

执行以下操作,

 ScrollView s = (ScrollView) findViewById(R.id.yourid);
 s.scrollTo(0,s.getTop());