За замовчуванням прапорець Android показує текст праворуч і прапорець ліворуч
Я хочу показати прапорець праворуч із текстом ліворуч
як я цього досягти?
За замовчуванням прапорець Android показує текст праворуч і прапорець ліворуч
Я хочу показати прапорець праворуч із текстом ліворуч
як я цього досягти?
Відповіді:
Я не можу придумати спосіб зі стилем, але ви можете просто встановити текст прапорця нанівець і поставити TextView зліва від прапорця з потрібним текстом.
Я думаю, що відповісти на це питання вже пізно, але насправді є спосіб досягти своєї мети. Вам просто потрібно додати наступний рядок у свій прапорець:
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
Ви також можете скористатись вашим персоналізованим шаблоном для перегляду для прапорця.
А для радіо-кнопки:
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
І якщо ви хочете це зробити програмно:
Визначте макет і назвіть RightCheckBox та скопіюйте наступні рядки:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:text="hello"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>
і коли вам потрібно додати його програмно, вам просто потрібно надути його до CheckBox і додати його до кореневого виду.
CheckBox cb = (CheckBox)((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.check_right_checkbox,null);
rootView.addView(cb);
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
. Я думаю, що це найкраще рішення для прапорця справа, яку я знайшов поки що.
android:drawableEnd
на додаток до android:drawableRight
(з однаковим значенням).
Ви можете зробити
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center"//or "center_vertical" for center text
android:layoutDirection="rtl"
android:text="hello" />
Наступного рядка достатньо
android:layoutDirection="rtl"
android:gravity="end|center_vertical"
щоб відображення тексту було ліворуч, оскільки макет починається прямо зараз.
Просто скопіюйте це:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text:"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
/>
</LinearLayout>
Щасливого кодування! :)
Текст прапорця може не вирівнюватися ліворуч
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
в якомусь пристрої. Можна використовувати CheckedTextView як заміну, щоб уникнути проблеми,
<CheckedTextView
...
android:checkMark="@android:drawable/btn_radio" />
і це посилання буде корисним: Вирівняти текст зліва, прапорець справа
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layoutDirection="rtl"
android:text="text" />`
Ви можете використовувати натомість CheckTextView.
http://developer.android.com/reference/android/widget/CheckedTextView.html
Додавання ще однієї відповіді на це питання, в якій використовується CheckedTextView Якщо хтось намагається це зробити програмно. Він також має можливість використовувати власні зображення для прапорця. І це можна зробити в одному представленні
final CheckedTextView checkBox = new CheckedTextView(getApplicationContext());
checkBox.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
checkBox.setId(1);
checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()){
checkBox.setChecked(false);
checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}else{
checkBox.setChecked(true);
checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
}
}
});
checkBox.setTextColor(Color.BLACK);
checkBox.setGravity(Gravity.LEFT);
checkBox.setText("hi");
З XML, якщо ви хочете ініціювати -
<CheckedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="@android:drawable/checkbox_off_background"
android:checked="false"
android:text="Hi from xml"/>
Наведене нижче посилання демонструє, як візуалізувати декілька стандартних об’єктів перегляду Android з анімованим прапором праворуч, встановивши правий параметр.
Встановіть фон, щоб отримати ефект пульсації.
[посилання на веб-сайт із прикладом прапорця з правої та лівої сторони.] [1] http://landenlabs.com/android/uicomponents/uicomponents.html#checkbox
<Button
android:id="@+id/p2Button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/transparent_ripple"
android:drawableRight="@drawable/checkline"
android:gravity="left|center_vertical"
android:text="Button"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/p2Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/transparent_ripple"
android:drawableRight="@drawable/checkline"
android:gravity="left|center_vertical"
android:text="AppCompatButton"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<TextView
android:id="@+id/p2TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/transparent_ripple"
android:drawableRight="@drawable/checkline"
android:gravity="left|center_vertical"
android:hapticFeedbackEnabled="true"
android:text="TextView"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/p2TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/transparent_ripple"
android:drawableRight="@drawable/checkline"
android:gravity="left|center_vertical"
android:hapticFeedbackEnabled="true"
android:text="AppCompatTextView"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/white" />
<CheckBox
android:id="@+id/p2Checkbox1"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:button="@null"
android:checked="true"
android:drawableRight="@drawable/checkline"
android:gravity="left|center_vertical"
android:text="CheckBox"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/p2Checkbox2"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:button="@null"
android:checked="true"
android:drawableRight="@drawable/checkline"
android:gravity="left|center_vertical"
android:text="AppCompatCheckBox"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<android.support.v7.widget.AppCompatCheckedTextView
android:id="@+id/p2Checkbox3"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:checkMark="@drawable/checkline"
android:checked="true"
android:gravity="left|center_vertical"
android:text="AppCompatCheckedTextView"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<!-- android:checkMark="?android:attr/listChoiceIndicatorMultiple" -->
<CheckedTextView
android:id="@+id/p2Checkbox4"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:checkMark="@drawable/checkline"
android:checked="true"
android:gravity="left|center_vertical"
android:text="CheckedTextView"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<CheckBox
android:id="@+id/p2Checkbox5"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:checked="true"
android:gravity="center_vertical|end"
android:text="CheckBox"
android:textColor="@android:color/white"
android:textSize="@dimen/buttonTextSize" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/white" />
<ToggleButton
android:id="@+id/p2ToggleButton1"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:checked="true"
android:drawableRight="@drawable/checkline"
android:gravity="center_vertical|left"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textOff="ToggleButtonOff"
android:textOn="ToggleButtonOn"
android:textSize="@dimen/buttonTextSize" />
<ToggleButton
android:id="@+id/p2ToggleButton2"
android:layout_width="match_parent"
android:layout_height="@dimen/buttonHeight"
android:background="@drawable/transparent_ripple"
android:checked="true"
android:drawableRight="@drawable/btn_check_material_anim"
android:gravity="center_vertical|left"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textOff="ToggleBtnnAnimOff"
android:textOn="ToggleBtnnAnimOn"
android:textSize="@dimen/buttonTextSize" />
Зразок checkline.xml (у тексті, який можна переглядати, див. Посилання на анімаційну версію в dravable-v21)
Зразок transparent_ripple.xml (у dravable-v21)
<!-- Limit ripple to view object, can also use shape such as oval -->
<item android:id="@android:id/mask" android:drawable="@android:color/white" />
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="200"
android:exitFadeDuration="200">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#80c0c000" />
</shape>
</item>
</selector>
</item>
Зразок transparent_ripple.xml (у малюванні, виділіть лише відсутність пульсацій)
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#80c0c000" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
Якщо це не обов'язкове використання a, CheckBox
ви можете просто використовувати Switch
замість цього. Перемикач показує текст зліва за замовчуванням.
Ви також можете використовувати це,
<CheckBox
android:layout_width="match_parent"
android:layout_height="@dimen/button_height_35"
android:text="@string/english"
android:checked="true"
android:paddingEnd="@dimen/padding_5"
android:paddingStart="@dimen/padding_5"
android:layoutDirection="rtl"
android:drawablePadding="@dimen/padding_5"
android:drawableEnd="@drawable/ic_english"
style="@style/TextStyleSemiBold"
android:textSize="@dimen/text_15"
android:button="@drawable/language_selector"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/location_permissions"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/location_permission_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:onClick="onLocationPermissionClicked" />
</RelativeLayout>
</LinearLayout>