1

紛らわしいヘッダーについてお詫びします。私の問題は次の画像でよりよく説明されています:ここに画像の説明を入力してください

緑色のボタンを画像の上部に揃える必要がありますが、画像は別のレイアウト内にあります。これは可能ですか?

必要に応じてコードで実行できます。XMLは必要ありません。Android2.2以降をターゲットにしています。

編集:

私の現在の実装は、ボタンのMarginTopプロパティを設定するだけですが、LinearLayout内のテキストのサイズを変更する必要がある場合、これは不便です。これは、画面サイズに応じて行う予定です。

どういうわけか、画像のY座標を見つけて、おそらくTextViewsの高さを追加し、これをボタンのMarginTopとして設定することで解決できると思いますが、これは面倒に聞こえます。本当に他の選択肢はありませんか?

LinearLayoutはViewPager内に配置されます(複数のビューがあり、すべて同じ位置に画像があります)。そのため、preeyaが説明するようにそれを行うことはできません。

4

2 に答える 2

1

次のように、画像とボタンを表示するためにlinearlayoutを使用できます。

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
          android:id="@+id/longText"
          android:gravity="center"
        android:text="Some very long text" />
       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/subtitle"
        android:layout_below="@+id/longText"
        android:text="subtitle" />
        <Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
                 android:layout_below="@+id/subtitle"   
                android:layout_toLeftOf="@+id/subtitle"
               android:layout_height="wrap_content"
               android:text="button" />


       <LinearLayout 
          android:layout_width="match_parent"
             android:layout_height="match_parent"
              android:layout_toRightOf="@+id/button1"
         android:layout_below="@+id/subtitle"   
         android:orientation="horizontal"
           >


           <ImageView
               android:id="@+id/imageView2"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:src="@drawable/ic_launcher" />

       </LinearLayout>

</RelativeLayout>
于 2013-01-13T17:52:59.027 に答える
1

可能ですが、ボタンを同じレイアウトに含めるよりも複雑です。絶対にそうしたくない場合は、XML を使用できません (XML の方が常に高速です)。コードで 3 つの手順を実行する必要があります。

1.) ビューが描画されるまで待ちます

private void waitForViewToBeDrawn(){
    // get your layout
    final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
    ViewTreeObserver vto = mainLayout.getViewTreeObserver();
    // add a listener
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            // you also want to remove that listener
            mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            // go on to next step
            getPositionOfImageView();

        }
    }); 
}

このアプローチが私には最適ですが、問題がある場合は、いくつかの代替手段があります。API レベル 11 以降を使用する場合、[その他の解決策][2] もあります...

2.) imageView の最上位を取得する

    private void getPositionOfImageView(){                
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        // Top position view relative to parent (Button and ImageView have same parent)
        int topCoordinate = imageView.getTop();
        adjustButton(topCoordinate);
    }

3.) 画像に合わせてボタンを追加または調整します

    public void adjustButton(int topCoordinate){
        Button button = (Button) findViewById(R.id.button);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = topCoordinate;
        button.setLayoutParams(params);
    }

API 11: button.setTop(topCoordinate)を使用すると、このステップがよりスムーズになります。

もちろん、すべてを短縮して単一の方法に入れることもできます.3つのステップで説明する方が良いと思いました. コードが開始に役立つことを願っています!

于 2013-01-14T11:49:04.517 に答える