我創建了一個按鈕類,因爲我需要按鈕具有特定的高度和寬度,具體取決於屏幕大小; 所以這裏是我的代碼:如何使用我自己的按鈕類來修復按鈕的寬度
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>
爲什麼你想在自定義視圖中做到這一點?爲什麼你要的按鈕是1/8的高度,併爲所有設備的屏幕寬度的1/6? – fifarunnerr
這裏你不需要定製按鈕來設置寬度和高度。你可以通過一個按鈕的LayoutParams來做到這一點... –