ApiDemos学习知识点Content-Rescoures(7)

本知识点分为几种不同的Resources


(一)LayoutReference

首先看一下LayoutReference 

布局和代码

演示了使用- wnnndp和- hnnndp资源配置。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0" android:gravity="center_horizontal"
        android:paddingTop="8dp" android:paddingBottom="8dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/resources_layout_reference_description"/>

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0" android:gravity="center_horizontal"
        android:paddingTop="8dp" android:paddingBottom="8dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/resources_layout_reference_default"/>

</LinearLayout>

这个布局使用不同的配置来进行调整根据将要发生的最小宽度显示的内容。

(二)ResourcesSample

这个Activity主要演示了怎样展示各种样式的资源。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/styled_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

    <TextView
        android:id="@+id/plain_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

    <TextView
        android:id="@+id/res1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

</LinearLayout>
public class ResourcesSample extends Activity {
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resources);

        TextView tv;
        CharSequence cs;
        String str;

        //使用getString()方便方法检索字符串但不能提供样式信息的资源。注意使用 CharSequence而不是字符串,所以我们不会丢失样式信息。

        cs = getText(R.string.styled_text);
        tv = (TextView)findViewById(R.id.styled_text);
        tv.setText(cs);
        // 使用相同的资源,但将其转换为字符串,这将导致它丢失样式信息。
        str = getString(R.string.styled_text);
        tv = (TextView)findViewById(R.id.plain_text);
        tv.setText(str);

        // 从我们的上下文中获取资源对象
        Resources res = context.getResources();

        // Get the string resource, like above.
        cs = res.getText(R.string.styled_text);
        tv = (TextView)findViewById(R.id.res1);
        tv.setText(cs);

        // 注意,资源类有类似getColor()的方法,getDimen(),getDrawable(),因为主题存储在资源中。你可以使用它们,但你可能想看一看风景例子来了解如何制作自定义小部件。

    }
}


(三)SmallestWidth

根据屏幕的宽去设置布局资源

本例的基本思路是: 
创建一个布局文件activity_row 
该布局中只是定义了两个文本框,通过layout_weight属性,设置两个TextView平均分配屏幕的宽度。 
需要注意的是这里使用了Merge节点。该节点可以使被include引用的布局在被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点。

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
            android:orientation="horizontal">
        <TextView android:layout_width="0px" android:layout_height="match_parent"
                android:layout_weight="1" android:gravity="center_horizontal"
                android:layout_marginLeft="4dp" android:layout_marginRight="4dp"
                android:background="#800000ff" android:text="Default Width\n#1">
        </TextView>
        <TextView android:layout_width="0px" android:layout_height="match_parent"
                android:layout_weight="1" android:gravity="center_horizontal"
                android:layout_marginLeft="4dp" android:layout_marginRight="4dp"
                android:background="#800000ff" android:text="Default Width\n#2">
        </TextView>
    </LinearLayout>
</merge>
activity_coluns.xml。在该布局中定义了两个均分屏幕剩余高度的FrameLayut。 
并在该FrameLayout使用include引入了上面设置的布局
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
            android:orientation="vertical">
        <FrameLayout android:layout_width="match_parent" android:layout_height="0px"
                android:layout_weight="1" android:padding="4dp"
                android:background="#8000ff00">
            <include layout="@layout/resources_smallest_width_row" />
        </FrameLayout>
        <FrameLayout android:layout_width="match_parent" android:layout_height="0px"
                android:layout_weight="1" android:padding="4dp"
                android:background="#80ff0000">
            <include layout="@layout/resources_smallest_width_row" />
        </FrameLayout>
    </LinearLayout>
</merge>
只是定义了一个TextView尔后将剩余的界面都归属了一个Framelayout.并在FrameLayout当中引入了activity_coluns.xml。这个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0" android:gravity="center_horizontal"
        android:paddingTop="8dp" android:paddingBottom="8dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/resources_smallest_width_description"/>

    <FrameLayout android:layout_width="match_parent" android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:drawable/gallery_thumb">
        <include layout="@layout/resources_smallest_width_inner" />
    </FrameLayout>

</LinearLayout>

(四)StyledText

展示了不同风格字符串资源

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/styled_text_rsrc"/>

    <TextView
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        android:text="@string/styled_text"/>

    <TextView
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />

    <TextView
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/styled_text_prog"/>

    <TextView android:id="@+id/text"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"/>

</LinearLayout>
<string name="activity_styled_text">Content/Resources/<i>Styled</i> <b>Text</b></string>
    <string name="styled_text_rsrc">Initialized from a resource:</string>
    <string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>
    <string name="styled_text_prog">Assigned programmatically:</string>

public class StyledText extends Activity
{
    @Override
	protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.styled_text);

        //以编程方式检索带有样式的字符串资源信息并应用于第二个文本视图。注意使用CharSequence而不是String,这样我们就不会丢失样式信息
        CharSequence str = getText(R.string.styled_text);
        TextView tv = (TextView)findViewById(R.id.text);
        tv.setText(str);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值