私がやろうとしていることについて、できるだけ明確にしようとします。Geopoint の助けを借りて最初に中央に配置された MapView があります。次に、この MapView の上に、いくつかの情報を表示する LinearLayout があります (linearlayout は mapview を部分的にカバーしています)。その幅は内容によって異なります。MapView を中央に保つために、linearlayout の幅を取得し、(ScrollBy メソッドを使用して) その幅の mapview を動的にスクロールできるようにしたいと考えています (linearlayout のコンテンツが変更されるたびに、mapview がスクロールされます)。
これが私がこれまでに行ったことです:
mapview とカスタム linearlayout を含む私の main.xml (私のアクティビティで使用されます) の抽出:
<com.mikecorp.weather.tab.myframe
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="@string/mapKey"
android:clickable="true"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="right"
android:orientation="horizontal">
<com.mikecorp.weather.tab.PanelLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/panel_weather_city"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical"
android:background="#FF4B4B4B" >
<TextView
android:id="@+id/curr_weather_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dip"
android:layout_marginRight="20dip"
android:paddingLeft="5dip"
android:gravity="center"
android:textSize="35dip"
android:textStyle="bold" >
</TextView>
<View android:id="@+id/divider_h"
android:background="@drawable/black_white_gradient"
android:layout_width="fill_parent"
android:layout_height="2dp"
/>
<TextView
android:id="@+id/curr_weather_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textStyle="bold"
android:paddingLeft="5dip"
android:paddingRight="20dip"
android:paddingTop="10dip"
android:paddingBottom="20dip"
>
</TextView>
<TextView
android:id="@+id/curr_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:layout_marginRight="20dip" >
</TextView>
</com.mikecorp.weather.tab.PanelLayout>
</LinearLayout>
</com.mikecorp.weather.tab.myframe>
ご覧のとおり、レイアウトの正しい幅を取得するために onLayout および onSizeChanged メソッドを定義したカスタム LinearLayout (PanelLayout) があります。そして、これはうまくいきます。正確なサイズがわかります。
public class PanelLayout extends LinearLayout {
private MapView mview;
private MapController controller;
public PanelLayout(Context context) {
super(context);
}
public PanelLayout(Context context, AttributeSet attrs){
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
Log.d("TEST", "onLayout:" + String.format("%s-%d,%d,%d,%d", changed, l, t, r, b));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.d("TEST", "onSizeChanged:" + String.format("%d,%d,%d,%d", w, h, oldw, oldh));
mview = (MapView) findViewById(R.id.mapView);
controller = mview.getController();
controller.scrollBy(w, 0);
}
}
しかし、プログラムを実行すると、NULL ポインター例外 (mview が null) が発生します。
その前に、アクティビティ自体で linearlayout の幅を取得 (getWidth メソッド) しようとしましたが、linearlayout がまだ作成されていないため、正しい値が得られません。そのため、カスタム LinearLayout で onSizeChanged メソッドを使用することにしました。しかし、ここで、私はブロックされています。
また、LinearLayout と MapView を含む ViewGroup (およびその onLayout メソッド) を使用することも考えましたが、LinearLayout の幅がいつ変更されるかはわかりません。
これが皆さんにとって理にかなっており、私を助けるのに十分な情報があることを願っています ;)