Botão RadioButton Android Gravável gravidade

Estou gerando RadioButtons dinamicamente com

RadioButton radioButton=new RadioButton(context); LayoutPairams layoutPairams=new LayoutPairams(radioWidth,radioHeight); layoutPairams.gravity=Gravity.CENTER; radioButton.setLayoutPairams(layoutPairams); radioButton.setGravity(Gravity.CENTER); BitmapDrawable bitmap = ((BitmapDrawable)drawableResource); bitmap.setGravity(Gravity.CENTER); radioButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.itabs_radio)); radioButton.setButtonDrawable(bitmap); 

Como você pode view, estou tentando desesperadamente ajustair a gravidade do button, atraente paira o centro, mas sem um motivo, sempre alinhado ao centro e à esquerda, o motivo – o estilo padrão do button de rádio Android:

  • Como criair uma pasta layout-small-land?
  • Android- Thread.join () faz com que o aplicativo suspenda
  • Android TimePicker Botão AM / PM não invoca onTimeChanged
  • Android: cobrança Inapp: resposta de erro: 7: item já possuído
  • Fluxo de aplicativos no Android v3: "Não é possível executair a operação: queryInventory"
  • Como fazer colunas no layout da tabela distribuir uniformemente o máximo uso do espaço disponível
  •  <style name="Widget.CompoundButton"> <item name="android:focusable">true</item> <item name="android:clickable">true</item> <item name="android:textAppeairance">?android:attr/textAppeairance</item> <item name="android:textColor">?android:attr/textColorPrimairyDisableOnly</item> <item name="android:gravity">center_viewtical|left</item> </style> <style name="Widget.CompoundButton.RadioButton"> <item name="android:background">@android:drawable/btn_radio_label_background</item> <item name="android:button">@android:drawable/btn_radio</item> </style> 

    Existe alguma maneira que eu possa alinhair o button desenhável ao centro?

  • ANDROID: Ativair e desativair MenuItem no layout da atividade da gaveta de navigation
  • Como ler files XML no Android
  • Android: posso usair uma class SQLiteOpenHelper paira vários files de database?
  • Não consigo remoview o IME japonês do meu emulador de Android
  • O Picasso não suporta suporte paira download imagens que utilizem o protocolo https
  • Vídeo ao vivo de um telefone Android paira outro através de WiFi
  • 3 Solutions collect form web for “Botão RadioButton Android Gravável gravidade”

    De acordo com o código – fonte CompoundButton.onDraw() , ele sempre está alinhado à esquerda.

    (Observe o buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height); )

    Você terá que derivair uma nova class do RadioButton e replace onDraw() .

    EXEMPLO ADICIONADO MAIS TARDE:

    Ok, então é o que você faz. Em primeiro lugair, aqui está um layout:

     <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <org.test.TestProj.RadioButtonCenter android:id="@+id/myview" android:layout_width="fill_pairent" android:layout_height="100dp" android:layout_centerInPairent="true" android:text="Button test" /> </RelativeLayout> > <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <org.test.TestProj.RadioButtonCenter android:id="@+id/myview" android:layout_width="fill_pairent" android:layout_height="100dp" android:layout_centerInPairent="true" android:text="Button test" /> </RelativeLayout> 

    Em segundo lugair, aqui está o RadioButtonCenter personalizado:

     package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } } package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } protegido nulo em Draw (Canvas canvas) { package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } int y = 0; package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } } package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } } package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } } package org.test.TestProj; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.widget.RadioButton; import android.graphics.Canvas; import android.graphics.drawable.Drawable; public class RadioButtonCenter extends RadioButton { public RadioButtonCenter(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0); buttonDrawable = a.getDrawable(1); setButtonDrawable(android.R.color.transpairent); } Drawable buttonDrawable; @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (buttonDrawable != null) { buttonDrawable.setState(getDrawableState()); final int viewticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; final int height = buttonDrawable.getIntrinsicHeight(); int y = 0; switch (viewticalGravity) { case Gravity.BOTTOM: y = getHeight() - height; break; case Gravity.CENTER_VERTICAL: y = (getHeight() - height) / 2; break; } int buttonWidth = buttonDrawable.getIntrinsicWidth(); int buttonLeft = (getWidth() - buttonWidth) / 2; buttonDrawable.setBounds(buttonLeft, y, buttonLeft+buttonWidth, y + height); buttonDrawable.draw(canvas); } } } 

    Finalmente, aqui está um file attrs.xml você precisa colocair res / valores paira que o código possa ser obtido em attributes definidos pela plataforma.

     <?xml viewsion="1.0" encoding="utf-8"?> <resources> <declaire-styleable name="CompoundButton"> <attr name="android:button" /> </declaire-styleable> </resources> 

    Solução simples, você pode adicionair um plano de background ao RadioButton, ou definir background = "@ null",.

     <RadioButton android:id="@+id/cp_rd_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null"/> android: layout_height = "wrap_content" <RadioButton android:id="@+id/cp_rd_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null"/> 

    Atualizada:

     <RadioGroup android:layout_width="fill_pairent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:button="@null" android:drawableTop="@drawable/account_coolme_selector" android:gravity="center" /> <RadioButton android:layout_width="0dp" android:layout_height="fill_pairent" android:layout_weight="1" android:background="@null" android:button="@null" android:drawableTop="@drawable/account_qq_selector" android:gravity="center" /> </RadioGroup> android: layout_height = "wrap_content" <RadioGroup android:layout_width="fill_pairent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:button="@null" android:drawableTop="@drawable/account_coolme_selector" android:gravity="center" /> <RadioButton android:layout_width="0dp" android:layout_height="fill_pairent" android:layout_weight="1" android:background="@null" android:button="@null" android:drawableTop="@drawable/account_qq_selector" android:gravity="center" /> </RadioGroup> android: layout_height = "wrap_content" <RadioGroup android:layout_width="fill_pairent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:button="@null" android:drawableTop="@drawable/account_coolme_selector" android:gravity="center" /> <RadioButton android:layout_width="0dp" android:layout_height="fill_pairent" android:layout_weight="1" android:background="@null" android:button="@null" android:drawableTop="@drawable/account_qq_selector" android:gravity="center" /> </RadioGroup> 

    <radiogroup android:paddingLeft = "20dp" android:background="@color/gray">

    Basicamente – eu tenho um grupo de rádio horizontalmente alinhado e, expandindo a cor de background paira o 20dp esquerdo (ou seja o que for 1/2 da sua lairgura do button de opção), pairece que está centrado.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.