デザイナー上でのビューのサイズの参照について

2,452 views
Skip to first unread message

NT

unread,
Jan 28, 2013, 9:37:41 PM1/28/13
to android-g...@googlegroups.com
失礼します。NTと申します。

サイズ調整する独自ビューを作りたいのですが
うまく実装できません。

端末でデバックするとビューを見ることができるのですが、
Eclipseのグラフィカルレイアウトでみようとしても以下のエラーメッセージが見えてみることができません。

Exception raised during rendering: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity

おそらく、コンテキストをキャストする際にエラーが起こっていると思うのですが、
この画面ではこの方法は使えないのでしょうか。
このような場合にどのようにすればよいか教えていただければ幸いです。
それでは失礼します。

public class DivLinearLayout extends LinearLayout {

    int nDivWidth;
    int nDivHeight;
    int nTarget;
   
    int nSaveWidth;
    int nSaveHeight;
    boolean bLoad;
   
    public DivLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        //attrs.xmlに定義したスタイルのインスタンスを作成
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DivLinearLayout);
       
        nDivWidth = a.getInt(0, 0);
        nDivHeight = a.getInt(1, 0);
        nTarget = a.getInt(2, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity)getContext();
        Window window = activity.getWindow();
        window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
       
        View v = findViewById(R.id.layout_parent);
       
        if(bLoad == false)
        {   
            nSaveWidth = metrics.widthPixels;
           
            nSaveHeight = metrics.heightPixels;
           
            bLoad = true;
        }
       
        if(nDivWidth > 1)
            getLayoutParams().width = nSaveWidth * nDivWidth / 100;
       
        if(nDivHeight > 1)
            getLayoutParams().height = nSaveHeight * nDivHeight / 100;
    }
   
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
       
    }
}


Hirokazu Fukami

unread,
Jan 28, 2013, 10:12:58 PM1/28/13
to android-g...@googlegroups.com
こんにちはfkmです。

> Activity activity = (Activity)getContext();
この行が原因だと思われます。getContext()はActivityを返すとは限らないので。

試してはいませんが、Activity経由ではなく、
Context.getSystemService(Context.WINDOW_SERVICE) で
WindowManagerを取得すればいいと思います。

2013/1/29 NT <take...@nikkodenshi.co.jp>:
> --
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com
> にメールを送信します。
> このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
> http://groups.google.com/group/android-group-japan?hl=ja
> からこのグループにアクセスしてください。
> その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。
>
>



--
-------------------------------------------------------------
深見 浩和(Hirokazu Fukami)

URI:http://www.fkmsoft.jp
email: f...@fkmsoft.jp

NT

unread,
Jan 29, 2013, 12:08:00 AM1/29/13
to android-g...@googlegroups.com
返信ありがとうございます。

エラーはなくなったのですがレイアウトが真っ白になりなにも映らなくなってしまいました。
(端末上では動作しております。)

ご協力ありがとうございます。
引き続きこちらでも調べながら投稿を募集したいと思います。


NT

unread,
Jan 29, 2013, 12:14:59 AM1/29/13
to android-g...@googlegroups.com

すみません。修正後のコードを載せるのを忘れておりました
現在はこのようなものになっております。


    int nDivWidth;
    int nDivHeight;
    int nTarget;
   
    int nSaveWidth;
    int nSaveHeight;
    boolean bLoad;
   
    public DivLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        //attrs.xmlに定義したスタイルのインスタンスを作成
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DivLinearLayout);
       
        nDivWidth = a.getInt(0, 0);
        nDivHeight = a.getInt(1, 0);
        nTarget = a.getInt(2, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
   
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
       
        if(bLoad == false)
        {   
            DisplayMetrics metrics = new DisplayMetrics();
            //ここを変更
            WindowManager window = (WindowManager)getContext().
                getSystemService(Context.WINDOW_SERVICE);
            window.getDefaultDisplay().getMetrics(metrics);

            nSaveWidth = metrics.widthPixels;           
            nSaveHeight = metrics.heightPixels;
            bLoad = true;
        }
       
        if(nDivWidth > 1)
            getLayoutParams().width = nSaveWidth * nDivWidth / 100;
       
        if(nDivHeight > 1)
            getLayoutParams().height = nSaveHeight * nDivHeight / 100;
    }

ちなみにレイアウトビューの中の記述はこちらです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res/test.sample">

    <test.sample.parts.DivLinearLayout
        android:layout_width="0px"
        android:layout_height="0px"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerInParent="true"
        android:background="#0000FF"
        app:height_div="50"
        app:width_div="50" >

    </test.sample.parts.DivLinearLayout>

</RelativeLayout>




Makoto Yamazaki

unread,
Jan 29, 2013, 1:52:08 AM1/29/13
to android-g...@googlegroups.com
zaki です。

View がDefault Display のサイズに依存して動作するというのがそもそも
間違っているような気がします。サイズを調整するのはそれを包含する
ViewGroup に任せるほうが自然なように思いますが、それではダメなの
でしょうか。


2013/1/29 NT <take...@nikkodenshi.co.jp>:
> --
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このグループから退会し、メールの受信を停止するには、android-group-j...@googlegroups.com
> にメールを送信します。
> このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
> http://groups.google.com/group/android-group-japan?hl=ja
> からこのグループにアクセスしてください。
> その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。
>
>



--
YAMAZAKI Makoto

NT

unread,
Jan 29, 2013, 2:22:34 AM1/29/13
to android-g...@googlegroups.com
> ViewGroup に任せるほうが自然なように思いますが

なるほどです。

だとすると、極端ですがこのようなビューのサイズを変化させたい場合は
どのようにすればよいのでしょうか。

間隔やビューの大きさを等倍率にディスプレイの大きさに合わせて変化させたいです。
ViewGroup に任せる場合はどのようにすればよいのでしょうか?
できればEclipseのGraphical Layoutで確認ができれば助かるのですが。
現在の端末はmdpi 1280 * 800です.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res/test.sample">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="91dp"
        android:text="テキスト1"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="テキスト3"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="85dp"
        android:text="テキスト2"
        android:textSize="20dp" />

</RelativeLayout>

Makoto Yamazaki

unread,
Jan 29, 2013, 2:35:44 AM1/29/13
to android-g...@googlegroups.com
zakI です。

Display のサイズではなく、親となる View を基準に等間隔の分割でも
問題ないのでしょうか。これで問題なければ GridLayout が使えるとおもいます。
参考: http://techbooster.org/android/application/9743/

上記の例は 4.0 以降ですが、これを Android 2.1 以降用にバックポートしたものが
support library の v7 として AOSPからリリースされています。

Android SDK の extras/android/support/v7/gridlayout/ にあります。
README.txt に使い方が書かれているのでみてください。


2013/1/29 NT <take...@nikkodenshi.co.jp>:

NT

unread,
Jan 29, 2013, 2:48:45 AM1/29/13
to android-g...@googlegroups.com

NTです。

なるほど、ありがとうございます。
早速試してみようと思います。

詳しく調べたいのでこちらの投稿は閉め切ろうと思います。
fkm様、zaki様ありがとうございました。
Reply all
Reply to author
Forward
0 new messages