[Android 界面] setContentView和inflate区别

一般用LayoutInflater做一件事:inflate

inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。
其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。

int resource,也就是resource/layout文件在R文件中对应的ID,这个必须指定。
而ViewGroup root则可以是null,null时就只创建一个resource对应的View,不是null时,会将创建的view自动加为root的child。

setContentView和inflate区别:
setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来
一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载
< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
***********************************************************
接下来结合源码说说inflate方法的四种形式:
inflate方法总共有四种形式,把xml表达的layout转化为view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on.
1. Context.public abstract object getSystemService(String name)
2. 两种获得LayoutInflater的方法
a. 通过SystemService获得
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
b. 从给定的context中获取
Public static LayoutInflater from(Context context)
c. 两者的区别:实际上是一样的,源码
/**
     * Obtains the LayoutInflater from the given context.
     */
    public static LayoutInflater from(Context context) {
        LayoutInflater LayoutInflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (LayoutInflater == null) {
            throw new AssertionError("LayoutInflater not found.");
        }
        return LayoutInflater;
}
3. LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。
4. LinearLayout linearLayout =
(LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text);
5. findViewById有两种形式
R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常
a. activity中的findViewById(int id)
b. View 中的findViewById(int id)
6.不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现Android聊天界面的步骤: 1. 创建一个新的Empty Activity,命名为MainActivity.java和activity_main.xml。 2. 在activity_main.xml中,使用LinearLayout或RelativeLayout作为根布局,然后在其中添加一个RecyclerView作为聊天消息的容器。 3. 创建一个适配器类(ChatAdapter)来管理聊天消息的显示。适配器需要继承RecyclerView.Adapter,并实现必要的方法,如onCreateViewHolder、onBindViewHolder和getItemCount。 4. 在ChatAdapter中,创建一个内部类ViewHolder来表示每个聊天消息的视图。ViewHolder需要继承RecyclerView.ViewHolder,并在构造函数中初始化视图的各个组件。 5. 在activity_main.xml中,为RecyclerView设置LayoutManager,并将ChatAdapter设置为RecyclerView的适配器。 6. 在MainActivity.java中,初始化RecyclerView和ChatAdapter,并将ChatAdapter与RecyclerView关联起来。 7. 在MainActivity.java中,创建一个List来存储聊天消息的数据。可以使用ArrayList或LinkedList来实现。 8. 在MainActivity.java中,通过调用ChatAdapter的方法来更新聊天消息的数据。可以通过添加新的消息到List中,并调用ChatAdapter的notifyItemInserted方法来实现。 9. 可以通过监听发送按钮的点击事件,在点击事件中获取输入框中的文本,并将其添加到聊天消息的数据List中。 10. 可以通过设置RecyclerView的滚动位置,使其自动滚动到最新的聊天消息。 11. 可以根据需要自定义聊天消息的布局和样式,如头像、时间戳等。 12. 可以添加其他功能,如发送图片、语音消息等。 下面是一个简单的示例代码: ```java // MainActivity.java public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private ChatAdapter chatAdapter; private List<String> chatMessages; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); chatMessages = new ArrayList<>(); chatAdapter = new ChatAdapter(chatMessages); recyclerView.setAdapter(chatAdapter); // 添加示例聊天消息 chatMessages.add("Hello"); chatMessages.add("Hi"); chatMessages.add("How are you?"); chatMessages.add("I'm fine, thank you!"); chatAdapter.notifyDataSetChanged(); } public void sendMessage(View view) { EditText editText = findViewById(R.id.editText); String message = editText.getText().toString(); chatMessages.add(message); chatAdapter.notifyItemInserted(chatMessages.size() - 1); recyclerView.scrollToPosition(chatMessages.size() - 1); editText.setText(""); } } ``` ```xml <!-- activity_main.xml --> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="16dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Type a message" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:onClick="sendMessage" /> </LinearLayout> </LinearLayout> ``` ```java // ChatAdapter.java public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> { private List<String> chatMessages; public ChatAdapter(List<String> chatMessages) { this.chatMessages = chatMessages; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_message, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { String message = chatMessages.get(position); holder.textViewMessage.setText(message); } @Override public int getItemCount() { return chatMessages.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView textViewMessage; public ViewHolder(@NonNull View itemView) { super(itemView); textViewMessage = itemView.findViewById(R.id.textViewMessage); } } } ``` ```xml <!-- item_chat_message.xml --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textViewMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:textSize="16sp" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值