dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.91k stars 525 forks source link

When set a style for an Android custom layout it didn't work, but in Android Studio it works fine #2156

Closed yorkvolvo closed 5 years ago

yorkvolvo commented 5 years ago

Steps to Reproduce

  1. Create a blank Xamarin.Android project.

  2. Create a style in values\styles.xml:

    
    <resources>
    
    <style name="RelativeLayout2">
    <item name="android:layout_width">40dp</item>
    <item name="android:layout_height">300dp</item>
    <item name="android:background">#114499</item>
    <item name="android:padding">50dp</item>
    </style>


3. Create a custom layout file:

namespace CustomeView { public class RelativeLayout2 : RelativeLayout { public RelativeLayout2(Context context, IAttributeSet attributeSet) : base(context, attributeSet, 0, Resource.Style.RelativeLayout2) { } } }

4. Use it in android `activity_main.axml` file:

<?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">

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="An utter lack of style."/>

</InflationWithStyle.RelativeLayout2>


5. In `MainActivity.cs`:
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
    }
}


<!--
If you have a repro project, you may drag & drop the .zip/etc. onto the issue editor to attach it.
-->

### Expected Behavior (When use these code in Android Studio)
![image](https://user-images.githubusercontent.com/18043540/45212274-0fe98b80-b2c7-11e8-973e-32ed970d295c.png)

### Actual Behavior(When use these code in Visual Studio)
![image](https://user-images.githubusercontent.com/18043540/45212346-40c9c080-b2c7-11e8-8737-bd3602972058.png)

## Question:

Could you please tell me that why defStyleRes has no effect in custom View (RelativeLayout)?

### Version Information
Microsoft Visual Studio Enterprise 2017 
Version 15.8.2
VisualStudio.15.Release/15.8.2+28010.2016
Microsoft .NET Framework
Version 4.7.03056

Installed Version: Enterprise

Architecture Diagrams and Analysis Tools   00369-60000-00001-AA644
Microsoft Architecture Diagrams and Analysis Tools

Visual C++ 2017   00369-60000-00001-AA644
Microsoft Visual C++ 2017

ADL Tools Service Provider   1.0
This package contains services used by Data Lake tools

Application Insights Tools for Visual Studio Package   8.13.10627.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017   15.8.05077.0
ASP.NET and Web Tools 2017

ASP.NET Core Razor Language Services   15.8.31590
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2017   5.2.60618.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   15.8.05023.0
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.3.4000.4
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   15.8.05023.0
Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio   2.3.4000.4
Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools   2.9.0-beta8-63208-01
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter   15.8.18201.1
Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Debugging Tools for Windows   10.0.17110.1002
Integrates the Windows Debugger functionality (http://go.microsoft.com/fwlink/?linkid=223405) in Visual Studio.

Dotfuscator Community Edition   5.36.0.7050-e77ce80a6
PreEmptive Protection - Dotfuscator CE

EMDKProfileManagerWizardVS   1.0
EMDK for Xamarin

Extensibility Message Bus   1.1.49 (remotes/origin/d15-8@ee674f3)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

GitHub.VisualStudio   2.5.4.3349
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IncrediBuild Build Acceleration   1.5.0.3
IncrediBuild effectively reduces compilation and development times by up to 90%.

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

Microsoft Azure HDInsight Azure Node   2.3.4000.4
HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service   2.3.4000.4
Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio   2.3
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service   2.3.4000.4
Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node   1.0
Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.10730.2

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of continuous build integration and continuous build delivery from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   1.0
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

MLGen Package Extension   1.0
MLGen Package Visual Studio Extension Detailed Info

Mono Debugging for Visual Studio   4.11.8-pre (009db7d)
Support for debugging Mono processes with Visual Studio.

Node.js Tools   1.4.20802.1 Commit Hash:97e1085d8b4b8e3e51c398e910177f87e86d135e
Adds support for developing and debugging Node.js apps in Visual Studio

NuGet Package Manager   4.6.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Python   15.8.18201.1
Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Python - Django support   15.8.18201.1
Provides templates and integration for the Django web framework.

Python - IronPython support   15.8.18201.1
Provides templates and integration for IronPython-based projects.

Python - Profiling support   15.8.18201.1
Profiling support for Python projects.

Redgate SQL Change Automation   3.0.18200.463
Extend DevOps processes to your SQL Server databases and safely automate database deployments.

Visit https://www.red-gate.com/sca/productpage for more information.

Copyright (C) 2011 Red Gate Software Ltd. All rights reserved.

This software contains components from Component Owl.
SQL Server is a registered trademark of Microsoft Corporation.
Visual Studio is a registered trademark of Microsoft Corporation.

SQL Change Automation contains code from the following open source software:

NuGet https://www.nuget.org/
SQL LocalDB Wrapper https://github.com/martincostello/sqllocaldb
Autofac https://autofac.org/
Json.NET https://json.net/
MahApps.Metro http://mahapps.com/
SemVer https://github.com/maxhauser/semver
Log4Net http://logging.apache.org/log4net/
StringTemplate https://github.com/antlr/stringtemplate4
Extended WPF Toolkit https://wpftoolkit.codeplex.com/
Code InfoBox VSX http://www.codeproject.com/Articles/55196/Code-InfoBox-Visual-Studio-Extension-VSX
OctoPack https://github.com/OctopusDeploy/OctoPack
SQLite https://sqlite.org/

This product contains icons from http://www.visualpharm.com distributed under a free backlink license.

For license details or other notices relating to the above software, please see NOTICE.TXT and EULA.rtf in the SQL Change Automation application folder.

Redgate SQL Prompt   9.2.6.6145
Write, format, and refactor SQL effortlessly

ResourcePackage Extension   1.0
ResourcePackage Visual Studio Extension Detailed Info

ResourcePackage Extension   1.0
ResourcePackage Visual Studio Extension Detailed Info

Snapshot Debugging Extension   1.0
Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools   15.1.61808.07020
Microsoft SQL Server Data Tools

Syntax Visualizer   1.0
An extension for visualizing Roslyn SyntaxTrees.

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript Tools   15.8.20801.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   2.9.0-beta8-63208-01
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android)   15.0.27924.00
Visual C++ for Cross Platform Mobile Development (Android)

Visual C++ for Cross Platform Mobile Development (iOS)   15.0.27825.00
Visual C++ for Cross Platform Mobile Development (iOS)

Visual C++ for Linux Development   1.0.9.27924
Visual C++ for Linux Development

Visual F# Tools 10.2 for F# 4.5   15.8.0.0.  Commit Hash: c55dd2c3d618eb93a8d16e503947342b1fa93556.
Microsoft Visual F# Tools 10.2 for F# 4.5

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for Apache Cordova   15.123.7408.1
Visual Studio Tools for Apache Cordova

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

Visual Studio Tools for Unity   3.8.0.6
Visual Studio Tools for Unity

Visual Studio Tools for Universal Windows Apps   15.0.28010.00
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Windows Driver Kit   10.0.17110.1002
Headers, libraries, and tools needed to develop, debug, and test Windows drivers (msdn.microsoft.com/en-us/windows/hardware/gg487428.aspx)

Xamarin   4.11.0.756 (d15-8@b66ff7180)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   4.14.221 (a73ca07f7)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   1.1.116 (9619170)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   9.0.0.19 (HEAD/a8a3b0ec7)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   11.14.0.13 (373c313)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

<!--
1. On macOS and within Visual Studio, select Visual Studio > About Visual Studio, then click the Show Details button, then click the Copy Information button.

2. Paste below this comment block.
-->

### Log File

<!--
1. On macOS and within Visual Studio:
    a. Click **Tools** > **SDK Command Prompt**.
    b. Within the launched `Terminal.app` window, run:

            adb logcat -d | pbcopy

2. Paste below this comment block
-->

<!--
Switch to the "Preview" tab to ensure your issue renders correctly.

Please add an appropriate "Area:" label in the Labels section.
-->
jonpryor commented 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.

yorkvolvo commented 5 years ago

@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>
yorkvolvo commented 5 years ago

@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.

jonathanpeppers commented 5 years ago

@aixiaozi it would help a lot if you could make a quick sample project showing the problem and attach to this issue, thanks!

grendello commented 5 years ago

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!