Est-il possible d'assigner une largeur de widget à la moitié de la largeur d'écran disponible, et de le faire en utilisant du xml déclaratif ?
Réponses
Trop de publicités?Si votre widget est un bouton :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somebutton">
<TextView android:layout_width="fill_parent"
android:layout_height="Wrap_content"
android:layout_weight="1">
</LinearLayout>
Voici comment je procéderais. Je suppose que vous voulez que votre widget occupe une moitié, et qu'un autre widget occupe l'autre moitié. L'astuce est d'utiliser un LinearLayout, en définissant layout_width="fill_parent"
sur les deux widgets, et en mettant layout_weight
à la même valeur sur les deux widgets également. S'il y a deux widgets, ayant tous deux le même poids, le LinearLayout répartira la largeur entre les deux widgets.
donnez une largeur de 0dp pour être sûr que sa taille corresponde exactement à son poids cela garantira que même si le contenu des vues enfant s'agrandit, il sera toujours limité à exactement la moitié (selon son poids).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="click me"
android:layout_weight="0.5"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World"
android:layout_weight="0.5"/>
</LinearLayout>