开发者

Help with Compund View and onLayout and onMeasure (HorizontalScrollView)

I want to create my own compound view. This view will extend a LinearLayout, and have a HorizontalScrollView in it. This HorizontalScrollView will hold a parent LinearLayout that will hold several TextView.

What I want to achieve is like an Alphabet Horizontal Index. With the above approach it works just fine. But it is not enough.

Right now letter A can not be at the center of the scroll, as it is the first letter, and you can not scroll more to the left. What I want to achieve is that the user can scroll to the left until the point where the first letter (A) is in the center. And that it works the same way when scrolling to the right and the last letter (Z).

I am very confused about how to do this. I know I can set padding to the inner LinearLayout in scroll, but I have to wait until I know how wide the scroll is (it's size is dynamic, FILL_PARENT). I can get this size in onLayout method, calling getMeasuredWidth. Then I can update the inner LinearLayout padding with the desired values, but it looks like eventhough I change the padding, the width of the innerLayout does not change.

I have also tried adding 2 spacers (just views) before A and after Z, but the paradigm is the same. I need to change its size when I know about the width of the scroll.

Please, help! :D

I can post some code if needed.

EDIT wit开发者_如何学Goh image:

Help with Compund View and onLayout and onMeasure (HorizontalScrollView)

So I want to be able to scroll more to the left, just until letter A is in the middle.

Help! Thanks.


Here is what I did for a test. Some screenshots here, here and here.

I think it matches quite well what you're trying to accomplish.

And some code for the activity AlphabetActivity.java

package com.ocus.androidtests.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Gallery;

import com.ocus.androidtests.R;

public class AlphabetActivity extends Activity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.alphabet);

        final Gallery gallery = (Gallery) findViewById(R.id.gallery);
        final String[] strings = new String[] {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        gallery.setAdapter(new ArrayAdapter<String>(this,
                R.layout.alphabet_item, strings));
    }
}

Code for layout alphabet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:spacing="20sp" android:unselectedAlpha="1"/>
</LinearLayout>

And code for adapter view (layout) alphabet_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="30sp" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜