Android Scrollview和布局问题

时间:2016-12-22 16:59:54

标签: android-layout

我对android布局有疑问。我无法实现我正在寻找的东西,我需要一些方向。 我正在寻找一个将在Scroll View布局中的布局。很难解释,但一张图片胜过千言万语。enter image description here

我想在滚动视图中找到它,请问有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

试试这个,我做了两个布局..

1)activity_main ,其中包含gridview和webview。

2)activity_gridview ,其中包含将在gridview中充气的imageview。

我已使用BaseAdapter动态添加图像到Gridview。

在MainActivity中,我已将该适配器设置为GridView。请尝试使用此代码。

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/linImages"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:numColumns="1"></GridView>

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toRightOf="@id/linImages"></WebView>
        </LinearLayout>


    </ScrollView>

</RelativeLayout>

activity_gridview.xml:

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

    <ImageView
        android:id="@+id/imgView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/star2" />

    <ImageView
        android:id="@+id/imgView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/star1" />

    <ImageView
        android:id="@+id/imgView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/star3" />

    <ImageView
        android:id="@+id/imgView4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/star1" />

</LinearLayout>

CustomAdapter.java:

public class CustomAdapter extends BaseAdapter {
    Context context;
    int flags[];
    LayoutInflater inflter;

    public CustomAdapter(Context applicationContext, int[] flags) {
        this.context = applicationContext;
        this.flags = flags;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return flags.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_gridview, null);
        ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
        ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
        ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
        ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);

        imgView1.setImageResource(flags[0]);
        imgView2.setImageResource(flags[1]);
        imgView3.setImageResource(flags[2]);
        imgView4.setImageResource(flags[3]);
        return view;
    }
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
    GridView grid;
    CustomAdapter customAdapter;
    WebView webView;

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

        webView= (WebView) findViewById(R.id.webView);
        grid = (GridView) findViewById(R.id.grid);
        customAdapter = new CustomAdapter(getApplicationContext(), images);
        grid.setAdapter(customAdapter);
    }
}

答案 1 :(得分:0)

如果你不想动态添加图像,那么这就是你想要的布局。试试这个。

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linMain"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <RelativeLayout
                android:id="@+id/lin1"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_margin="10dp">

                <ImageView
                    android:id="@+id/imgView1"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="@android:color/holo_purple" />

                <ImageView
                    android:id="@+id/imgView2"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_below="@id/imgView1"
                    android:layout_marginTop="20dp"
                    android:background="@android:color/holo_red_light" />

                <ImageView
                    android:id="@+id/imgView3"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_below="@id/imgView2"
                    android:layout_marginTop="20dp"
                    android:background="@android:color/holo_green_light" />

                <ImageView
                    android:id="@+id/imgView4"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_below="@id/imgView3"
                    android:layout_marginTop="20dp"
                    android:background="@android:color/holo_blue_light" />
            </RelativeLayout>

            <LinearLayout
                android:id="@+id/lin2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:orientation="vertical">

                <WebView
                    android:id="@+id/webView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_toRightOf="@id/linImages"></WebView>

            </LinearLayout>


        </LinearLayout>

    </ScrollView>

</RelativeLayout>

这里我附上了这个布局UI的截图。

enter image description here

相关问题