Hello,
Welcome to our Microsoft Q&A platform!
If you want to set the fontsize
with a float property. I used the following way.
Firstly, I add a float property called fontScale
in the attrs.xml
.
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="integer" />
<attr name = "fontScale" format = "float" />
</declare-styleable>
</resources>
Then I set it in the XML.
<App55.MyTextView
android:layout_width="200dp"
android:layout_height="200dp"
app:testAttr="520"
app:text="helloworld"
app:fontScale="11.5"
android:text="TestTestTestTestTestTestTestTestTestTestTestTestTestTest"
/>
We can get the value app:fontScale
then set it to the custom control.
public class MyTextView : TextView
{
protected MyTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public MyTextView(Context context) : base(context)
{
Initialize(context);
}
public MyTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
Initialize(context, attrs);
}
public MyTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
Initialize(context, attrs);
}
public MyTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
Initialize(context, attrs);
}
float ontScale;
private void Initialize(Context context, IAttributeSet attrs = null)
{
if (attrs != null)
{
TypedArray ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.test);
string text = ta.GetString(Resource.Styleable.test_text);
int textAttr = ta.GetInt(Resource.Styleable.test_testAttr, 0);
ontScale = ta.GetFloat(Resource.Styleable.test_fontScale, 0);
//set float value to the custom control‘s TextSize
this.TextSize = ontScale;
ta.Recycle();
}
}
}
}
Here is running screenshot.
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.