2014-01-19 25 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按钮适合 –

相关问题