text 简单的片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 简单的片段相关的知识,希望对你有一定的参考价值。
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.android_me.ui.AndroidMeActivity">
<!-- LinearLayout for holding three Android-Me images -->
<LinearLayout
android:id="@+id/android_me_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<!-- This container holds the head BodyPartFragment of the custom Android-Me image -->
<FrameLayout android:id="@+id/head_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>
</LinearLayout>
</ScrollView>
package com.example.android.android_me.ui;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.android.android_me.R;
// This activity will display a custom Android image composed of three body parts: head, body, and legs
public class AndroidMeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_me);
// Create a new head BodyPartFragment
BodyPartFragment headFragment = new BodyPartFragment();
// Add the fragment to its container using a FragmentManager and a Transaction
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.head_container, headFragment)
.commit();
}
}
1. Create a fragment layout
2. Create a class that extends Fragment
3. Inflate the view
4. Add fragment to the main layout file
5. Bind object of the fragment class in the main activity
package com.example.android.android_me.ui;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.android.android_me.R;
import com.example.android.android_me.data.AndroidImageAssets;
public class BodyPartFragment extends Fragment {
/**
* Mandatory empty constructor for the fragment manager to instantiate the fragment
*/
public BodyPartFragment() {
}
/**
* Inflates the fragment layout file and sets the correct resource for the image to display
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the Android-Me fragment layout
View rootView = inflater.inflate(R.layout.fragment_body_part, container, false);
// Get a reference to the ImageView in the fragment layout
ImageView imageView = (ImageView) rootView.findViewById(R.id.body_part_image_view);
// Set the image to the first in our list of head images
imageView.setImageResource(AndroidImageAssets.getHeads().get(0));
// Return the rootView
return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- This fragment layout displays just one image of an Android-Me body part -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/body_part_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
以上是关于text 简单的片段的主要内容,如果未能解决你的问题,请参考以下文章