Я создал класс кнопок, потому что мне нужны кнопки, чтобы иметь определенную высоту и ширину в зависимости от размера экрана; так вот мой код:Как исправить ширину кнопки с помощью моей собственной кнопки класс
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 кнопки ... –