gpt4 book ai didi

android - 如何用我自己的按钮类来固定按钮的宽度

转载 作者:行者123 更新时间:2023-11-30 03:09:33 31 4
gpt4 key购买 nike

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

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>

最佳答案

您可以通过将此代码放在 Activity 的 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。

关于android - 如何用我自己的按钮类来固定按钮的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216529/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com