断片

2016-05-27 56 views
4

にボタンの幅/高さを取得するために、どのように私は、フラグメントに任意のビュー(ボタン、TextViewの、RelativeLayout)の高さ/幅を取得することができますどのように、私はその断片

public static class FirstDemoFragment extends Fragment { 

    int width,height; 
    private Button button; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_layout, container, false); 
     button = (Button) view.findViewById(R.id.fragment_demo_button); 

     width = button.getWidth(); 
     height = button.getHeight(); 


     System.out.println("==== View Width : " + width); 
     System.out.println("==== View height : " + height); 

     width = button.getMeasuredWidth(); 
     height = button.getMeasuredHeight(); 


     System.out.println("==== View Width : " + width); 
     System.out.println("==== View height : " + height); 

     return view; 
    } 

私layout.xmlファイルのようなものを試してみてください私はその上で少し研究を持って

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

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/does_really_work_with_fragments" 
    android:id="@+id/fragment_demo_button" 
    android:layout_centerInParent="true" /> 

ですが、私はこの方法を使用して活動中のボタンの幅/高さを発見しました。

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
    relMain = (RelativeLayout) findViewById(R.id.layoutMain); 

    width = relMain.getMeasuredWidth(); 
    height = relMain.getMeasuredHeight(); 

    width = relMain.getWidth(); 
    height = relMain.getHeight(); 

    //System.out.println("=== Width : " + width); 
    //System.out.println("=== height : " + height); 


} 

ただし、onWindowFocusChanged()はAndroid Fragmentでは使用できません。フラグメントで

+0

アプリケーションがクラッシュしますか? – mubeen

答えて

3

button = (Button) view.findViewById(R.id.fragment_demo_button); 
    ViewTreeObserver vto = button.getViewTreeObserver(); 
     vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
      public boolean onPreDraw() { 
       button.getViewTreeObserver().removeOnPreDrawListener(this); 
       Log.d("", "Height: " + button.getMeasuredHeight()+ " Width: " + button.getMeasuredWidth()); 
       return true; 
      } 
     }); 
+0

@Gaurav Mandlik助けてくれれば回答を投票してください。 –

+0

希望の出力を達成するのに役立ちました。ありがとう兄貴 – Mrinmoy

1

は、フラグメントのonCreateViewメソッド内のコードの下に使用

ViewTreeObserver mviewtreeobs = button.getViewTreeObserver(); 
mviewtreeobs .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
    width = button.getWidth(); 
    height = button.getHeight(); 
} 
}); 
0

あなたはそうのようなあなたのボタンにレイアウトリスナーを追加することができます。フラグメントでフラグメントのonCreateViewメソッド内のコードの下にしようと

button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      int width = button.getWidth(); 
      if(width > 0){ 
       int height = button.getHeight(); 
       // do something with your width and height 
       .... 
       // remove layout listener 
       button.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
      } 
     }