【问题标题】:How to add style for dynamic layout如何为动态布局添加样式
【发布时间】:2017-06-12 07:03:17
【问题描述】:

你能帮帮我吗?我有相对布局,在这个布局中有 2x 文本视图,左边一个图片框,一个右边一个图片框,我需要添加这个动态组合,但我仍然有这个错误。

我的代码有错误:

这是我的样式 (Resource/Value/style.xml) XML 样式

我有人可以帮助我吗?谢谢

【问题讨论】:

标签: android xml xamarin dynamic add


【解决方案1】:

样式和背景资源都不同

所以你必须像下面这样写

RelativeLayout someLayout = new RelativeLayout(context, null, R.style.mystyle);

【讨论】:

  • 请你告诉我 setBackgroundResource(R.style.Folder_Style_LineColor); 之间有什么区别?和 .SetBackgroundResource(Resource.Style.Folder_Style_LineColor‌​);
  • “资源”来自 android.content.res,“R”来自您的应用包
  • 请再次检查亲爱的我说你不能从后台资源设置样式使用我在那里设置的内容
  • RelativeLayout someLayout = new RelativeLayout(context, null, R.style.mystyle);
  • 我正在使用 xamarin.android 这就是为什么这段代码对我不起作用的问题吗? (context, null, R.style.mystyle);
【解决方案2】:

Ide 在您的代码中找不到 Resource 文件夹。试试这个。

_line.setBackgroundResource(R.style.Folder_Style_LineColor);

【讨论】:

  • 请你告诉我 setBackgroundResource(R.style.Folder_Style_LineColor); 之间有什么区别?和 .SetBackgroundResource(Resource.Style.Folder_Style_LineColor);
【解决方案3】:

我的代码:

MainActivity.cs

RelativeLayout _tempLay = new RelativeLayout(this);

_tempLay.SetBackgroundResource(Resource.Style.Folder_Style);

ImageView _line = new ImageView(this);

_line.SetBackgroundResource(Resource.Style.Folder_Style_LineColor);

TextView _title = new TextView(this);
_title.SetBackgroundResource(Resource.Style.Folder_Style_Title);
_title.Text = "My Title";

TextView _des = new TextView(this);
_des.SetBackgroundResource(Resource.Style.Folder_Style_Description);
_des.Text = "Description...";

_tempLay.AddView(_line);
_tempLay.AddView(_title);
_tempLay.AddView(_des);

LinearLayout lal = this.FindViewByIdL<LinearLayout>(Resource.Id.linearLayout1);
lal.AddView(_tempLay);

资源/值/style.xm

<style name="Folder_Style" parent="@android:Theme.Holo.Light">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">75dp</item>
    <item name="android:background">@color/Gray_75</item>
  </style>

  <style name="Folder_Style_LineColor" parent="@android:Theme.Holo.Light">
    <item name="android:layout_width">15dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:background">@color/Blue_500</item>
  </style>

  <style name="Folder_Style_Title" parent="@android:Theme.Holo.Light">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">40dp</item>
    <item name="android:textSize">25dp</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:textColor">@color/BackGround_Title</item>
    <item name="android:layout_marginLeft">25dp</item>
    <item name="android:layout_marginRight">55dp</item>
  </style>

  <style name="Folder_Style_Description" parent="@android:Theme.Holo.Light">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">30dp</item>
    <item name="android:textSize">15dp</item>
    <item name="android:gravity">top</item>
    <item name="android:textColor">@color/Gray_700</item>
    <item name="android:layout_marginLeft">25dp</item>
    <item name="android:layout_marginRight">55dp</item>
    <item name="android:layout_marginTop">40dp</item>
  </style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多