단추 크기가 화면 크기에 따라 달라 지므로 버튼 클래스를 만들었습니다. 그것이 내가해야 할 말은 드로어 블 크기에 따라 변경되지 않도록 내가 높이와 너비를 해결할 수있는 방법을 내가내 단추 클래스로 단추의 너비를 고정하는 방법
<pachagename.MyButtons
android:id="@+id/access"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/aw"
android:layout_marginRight="10dp"
/>
이
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의 : 그래서 여기 내 코드입니다 그것은 mybuttons 클래스에 추가 된 것으로 ... 하지만 이제는 wrap_content이기 때문에 drawable 크기에 따라 여전히 변경됩니다. 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>
당신은 당신의 활동의) (하지 OnCreate이 코드를 넣어 바닥있는 LinearLayout의 높이와 너비를 설정할 수 있습니다
왜 사용자 정의보기에서 정확히 이것을 수행 하시겠습니까? 그리고 왜 버튼을 높이의 1/8로, 모든 장치의 화면 너비의 1/6로 만들고 싶습니까? – fifarunnerr
여기 너비와 높이를 설정하기위한 버튼을 사용자 정의 할 필요가 없습니다. 당신은 버튼의 LayoutParams를 통해 그것을 할 수 있습니다 ... –