作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个按钮类,因为我需要按钮根据屏幕尺寸具有特定的高度和宽度;所以这是我的代码:
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/
我是一名优秀的程序员,十分优秀!