1. 程式人生 > >安卓佈局---ScrollView上下滑動

安卓佈局---ScrollView上下滑動

ScrollView是一個頂層佈局,可以實現大於手機螢幕的佈局,從而實現頁面上下滑動

看程式碼就會理解

.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >


    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="100px" android:orientation="horizontal"> <ImageView android:layout_width="80px" android:layout_height="match_parent" android:id=
"@+id/headimg2"/> <TextView android:layout_width="300px" android:layout_height="match_parent" android:id="@+id/username2" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp"/> </LinearLayout> <View android:layout_width="fill_parent" android
:layout_height="1px" android:background="?android:attr/listDivider" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/comment2" android:layout_marginLeft="10dp"/> <ImageView android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/food1" android:padding="10dp" android:scaleType="fitXY"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/food2" android:padding="10dp" android:scaleType="fitXY"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/food3" android:padding="10dp" android:scaleType="fitXY"/> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/feed" android:padding="10dp"/> </LinearLayout> </ScrollView>

.java檔案

package com.example.administrator.test2application;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class jumpnew2Activity extends Activity {

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


         ImageView headpic= findViewById(R.id.headimg2);
          TextView username = findViewById(R.id.username2);
        TextView commentwords= findViewById(R.id.comment2);
        ImageView foodpic1=findViewById(R.id.food1);
        ImageView foodpic2=findViewById(R.id.food2);
        ImageView foodpic3=findViewById(R.id.food3);
        TextView feed=findViewById(R.id.feed);

        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();

        int headimg=bundle.getInt("headpic");
        headpic.setImageResource(headimg);

        String username1=bundle.getString("username");
        username.setText(username1);

        String commentwords1=bundle.getString("commentwords");
         commentwords.setText(commentwords1);

        int foodimg1=bundle.getInt("foodpic1");
        foodpic1.setImageResource(foodimg1);

        int foodimg2=bundle.getInt("foodpic2");
        foodpic2.setImageResource(foodimg2);

        int foodimg3=bundle.getInt("foodpic3");
         foodpic3.setImageResource(foodimg3);

        feed.setText("                                還沒人?趕緊搶沙發!        " +"\n"+
                "                  據說第一個評論的人都有枚幸運草");


    }
}

上述.java程式碼中的intent和bundle是用於資料接收的,讀者可以用其他方法或者直接寫死也行,重點在佈局這塊。

下面是效果截圖,由於沒有動圖,所以就截圖截了兩張,讀者將就著看。