Android C # TextView.setGravity

first question here.

I am trying to develop an application using Xamarin and the target android from C #. My problem starts when I create an instance of TextView and try to manipulate it with code:

text = new TextView(context); text.LayoutParameters = new GridView.LayoutParams(hwidth, hheight); text.SetPadding(8, 8, 8, 8); 

now I am reading the Android API and I need to change the gravity of this Textview so that the text is in the center, but when I try:

 text.setGravity(Gravity.CENTER); 

These are just errors saying that Android.Widget.TextView does not contain a definition for 'setGravity'. Am I missing something ridiculously simple or what?

All help appreciated!

+4
source share
2 answers

As @jarvis noted in the comment

 TextView.Gravity = GravityFlags.Center; 

This will work.

+6
source

You should set gravity gravity instead of gravity, you can try:

 params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER); 

xamarin allows you to use xml files? If yes, you can set gravity to:

 <TextView android:id="@+id/smiley" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> 

Hope for this help

+1
source

Source: https://habr.com/ru/post/1485453/


All Articles