2014-01-19 62 views
1

我創建了一個按鈕類,因爲我需要按鈕具有特定的高度和寬度,具體取決於屏幕大小; 所以這裏是我的代碼:如何使用我自己的按鈕類來修復按鈕的寬度

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.WindowManager; 
import android.widget.Button; 

public class MyButtons extends Button { 

     public MyButtons(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      DisplayMetrics displaymetrics = new DisplayMetrics(); 
      WindowManager windowManager = (WindowManager) context 
        .getSystemService(Context.WINDOW_SERVICE); 
      windowManager.getDefaultDisplay().getMetrics(displaymetrics); 
      int height = displaymetrics.heightPixels; 
      int width = displaymetrics.widthPixels; 

      setHeight(height/8); 
      setWidth(width/6); 

      Log.d("btheight",height*1/8+"");  

     } 


} 

在XML filde我

<pachagename.MyButtons 
     android:id="@+id/access" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"  
     android:background="@drawable/aw" 
     android:layout_marginRight="10dp" 
     /> 

如何解決的高度和寬度,以便它不依賴於繪製大小我的意思是,我需要改變它是在mybuttons類中添加的... 但現在它依賴於可繪製大小仍然在變化,因爲它是wrap_content,所以我應該用什麼來代替它來告訴它採用Mybuttins類指定的寬度和高度?

我的佈局是:

<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" 

    tools:context=".MainActivity" 
    android:background="@drawable/app_cover" 

    > 

    <LinearLayout 
     android:id="@+id/L1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/L2" 
     android:layout_centerHorizontal="true" 
     >  
     <packagename.MyButtons 
     android:id="@+id/access_awkat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"  
     android:background="@drawable/awkat" 
     android:layout_marginRight="10dp" 
     /> 

    <packagename.MyButtons 
     android:id="@+id/access_mofadala" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"  
     android:background="@drawable/mofadala" 
     android:layout_marginRight="10dp" 
     />  

    <packagename.MyButtons 
     android:id="@+id/access_introduction" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:background="@drawable/mokadima" 
     /> 
     </LinearLayout> 

<LinearLayout 
     android:id="@+id/L2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="1dp" 

     > 

    <packagename.MyButtons 
     android:id="@+id/about_program" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/bernamej" 
     android:layout_marginRight="10dp" 
     /> 

    <packagename.MyButtons 
     android:id="@+id/user_settings" 
    android:layout_width="wrap_content" 
     android:layout_height="wrap_content"    
     android:background="@drawable/edadat" 
     android:layout_marginRight="10dp" 
     /> 
    <packagename.MyButtons 
     android:id="@+id/access_remembrances" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:background="@drawable/azkar" 

     /> 



    </LinearLayout> 

</RelativeLayout> 
+0

爲什麼你想在自定義視圖中做到這一點?爲什麼你要的按鈕是1/8的高度,併爲所有設備的屏幕寬度的1/6? – fifarunnerr

+0

這裏你不需要定製按鈕來設置寬度和高度。你可以通過一個按鈕的LayoutParams來做到這一點... –

回答

1

您可以通過將在OnCreate代碼()你的活動設置底部的LinearLayout的高度和寬度:

DisplayMetrics displaymetrics = new DisplayMetrics(); 
WindowManager windowManager = (WindowManager) context 
       .getSystemService(Context.WINDOW_SERVICE); 
windowManager.getDefaultDisplay().getMetrics(displaymetrics); 
int height = displaymetrics.heightPixels; 
int width = displaymetrics.widthPixels; 

View view = findViewById(R.id.L2); 
ViewGroup.LayoutParams params = view.getLayoutParams(); 
params.height = height/8; 
params.width = width/6; 
view.setLayoutParams(params); 

請務必更改該佈局中所有按鈕的layout_height爲「fill_parent」,然後它們都應該是屏幕高度的1/8。

+0

我有一個特定的圖像,我想要的按鈕是在圖像的按鈕,圖像包含文本,但在buttom它有一個1/8的自由空間,所以在這裏我想顯示我的按鈕..這就是我正在做的.. –

+2

你很可能可以用RelativeLayout完成所有這些操作,並且不需要自定義的MyButtons類,也不需要使用LayoutParams。也許你應該發佈你的layout.xml,如果你正在使用一個 – MikeHelland

+0

好吧我把我的相對佈局,它包含我的按鈕,請你能告訴我如何使我的屏幕的1/8按鈕適合 –

相關問題