【问题标题】:How to fix the width of the button with my own button class如何用我自己的按钮类修复按钮的宽度
【发布时间】:2014-02-08 14:11:46
【问题描述】:

我创建了一个按钮类,因为我需要按钮根据屏幕尺寸具有特定的高度和宽度; 所以这是我的代码:

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 文件中

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

【问题讨论】:

  • 为什么要在自定义视图中执行此操作?为什么你希望按钮的高度是所有设备的 1/8,屏幕宽度的 1/6?
  • 这里你不需要自定义按钮来设置宽度和高度。您可以通过 Button 的 LayoutParams 来做到这一点...

标签: android button height width drawable


【解决方案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。

【讨论】:

  • 我有一个特定的图像,我希望按钮位于该图像的底部,并且图像包含文本,但在底部它有 1/8 的可用空间,所以我想在这里显示我的按钮..这就是我正在做的..
  • 您很可能可以使用RelativeLayout 完成所有这些操作,无需自定义MyButtons 类,也无需使用LayoutParams。如果你正在使用一个,也许你应该发布你的 layout.xml
  • 好的,我放置了包含按钮的相对布局,请您告诉我如何使按钮适合屏幕的 1/8
  • 好的,我更新了我的答案,希望可以解释它。
  • 我编辑了代码以显示您可以像设置高度一样设置宽度。另一种实现方法是将每个按钮的宽度设置为“0dp”,然后将权重设置为“1.0”。还将 LinearLayout 的宽度设置为“fill_parent”。然后布局充气器将使每个按钮的宽度相同,直到它们填满屏幕。
猜你喜欢
  • 2019-12-25
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 2012-12-10
  • 2021-03-15
  • 2014-01-23
相关资源
最近更新 更多