Closed yorkvolvo closed 5 years ago
Could you please attach both the Xamarin.Android project and the Android Studio projects?
I find this confusing, because neither the Styles and Themes documentation nor the Style resource documentation imply that the //style/@name
value can be a View
name and have things "magically linked". Instead, they both use a style
attribute on the View, e.g.
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="@style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
The original description does not use a style
attribute in this manner.
@jonpryor ,
Code in Android Studio:
public class RelativeLayout2 extends RelativeLayout
{
public RelativeLayout2(Context context, AttributeSet attributeSet)
{
super(context, attributeSet, 0, R.style.RelativeLayout2);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.example.testproject.RelativeLayout2
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="An utter lack of style."/>
</com.example.testproject.RelativeLayout2>
@jonpryor I think problem is the line base(context, attributeSet, 0, Resource.Style.RelativeLayout2)
didn't work.
I have searched a lot about android custom layout set style programmatically and found that we could set custom layout style like this:
public class RelativeLayout2 : RelativeLayout
{
public RelativeLayout2(Context context, IAttributeSet attributeSet)
: base(context, attributeSet, 0, Resource.Style.RelativeLayout2)
{
}
}
And it did works fine in Android Studio:
public class RelativeLayout2 extends RelativeLayout
{
public RelativeLayout2(Context context, AttributeSet attributeSet)
{
super(context, attributeSet, 0, R.style.RelativeLayout2);
}
}
But the same code in C# didn't work for Xamarin.Android
, I wonder why this issue happened? Is this a bug of Xamarin.Android
?
If you have any problem when reproduce this issue, please feel free to contact me.
@aixiaozi it would help a lot if you could make a quick sample project showing the problem and attach to this issue, thanks!
Since no requested info has been provided to-date, I'm going to close this issue. Please reopen if the requested information is available, thanks!
Steps to Reproduce
Create a blank Xamarin.Android project.
Create a style in
values\styles.xml
:namespace CustomeView { public class RelativeLayout2 : RelativeLayout { public RelativeLayout2(Context context, IAttributeSet attributeSet) : base(context, attributeSet, 0, Resource.Style.RelativeLayout2) { } } }
<?xml version="1.0" encoding="utf-8"?> <InflationWithStyle.RelativeLayout2 xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
</InflationWithStyle.RelativeLayout2>