android自訂LinearLayout和View

來源:互聯網
上載者:User

標籤:

自訂線性布局經常用到:

第一種是在擴充的LinearLayout建構函式中使用Inflater載入一個布局,並從中提取出相關的UI組件進行封裝,形成一個獨立的控制項。在使用該控制項時,由於它所有的子項目都是在運行時通過代碼動態建立的,所以該控制項只能以一個獨立控制項的形式在Layout檔案中聲明,例如:

public class CustomLayout extends LinearLayout{       public  CustomLayout(Context context){                 LayoutInflater mInflater = LayoutInflater.from(context);                View myView = mInflater.inflate(R.layout.receive, null);                addView(myView);       }}
< LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  androidundefinedrientation="vertical" >  <LinearLayout           android:layout_width="wrap_content"          android:layout_height="wrap_content"          androidundefinedrientation="horizontal">          <TextView                   android:layout_width="wrap_content"                  android:layout_height="wrap_content" />         <Button                 android:layout_width="wrap_content"                  android:layout_height="wrap_content"                   android:id="@+id/button" />  </LinearLayout>             <TextView                   android:layout_width="wrap_content"                  android:layout_height="wrap_content" />                           < /LinearLayout>

執行個體:

imagebtn.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="46dp"        android:layout_height="46dp"                android:layout_gravity="center_vertical"        android:layout_marginRight="10dp"        android:src="@drawable/confirm" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:text="確定"        android:textSize="25dp" /></LinearLayout>
MyLinearLayout1.java

package com.hust.customlinearlayout;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MyLinearLayout1 extends LinearLayout {private ImageView imageView;private TextView  textView;public MyLinearLayout1(Context context){super(context);}public MyLinearLayout1(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.imagebtn, this);imageView=(ImageView) findViewById(R.id.imageView1);textView=(TextView)findViewById(R.id.textView1);}  public void setImageResource(int resId){imageView.setImageResource(resId);}public void setTextViewText(String text){textView.setText(text);}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal"    android:orientation="horizontal" >    <com.hust.customlinearlayout.MyLinearLayout1        android:id="@+id/btn_right"        android:layout_height="wrap_content"          android:layout_width="wrap_content"        android:layout_weight="1"        />    <com.hust.customlinearlayout.MyLinearLayout1        android:id="@+id/btn_error"        android:layout_marginLeft="5dp"         android:layout_weight="1"        android:layout_height="wrap_content"          android:layout_width="wrap_content"               /></LinearLayout>
package com.hust.customlinearlayout;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Toast;public class MainActivity extends ActionBarActivity {    private MyLinearLayout1 myLinearLayout1;    private MyLinearLayout1 myLinearLayout2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myLinearLayout1=(MyLinearLayout1) findViewById(R.id.btn_right);myLinearLayout2=(MyLinearLayout1) findViewById(R.id.btn_error);myLinearLayout1.setTextViewText("確定");myLinearLayout2.setTextViewText("取消");myLinearLayout1.setImageResource(R.drawable.confirm);myLinearLayout2.setImageResource(R.drawable.cancle);myLinearLayout1.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "點擊的正確按鈕", 1).show();}});myLinearLayout2.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "點擊的錯誤按鈕", 1).show();}});}}


第二種方式是:這個自訂VIEW中的任何控制項都不是通過XML檔案來定義的,而是在JAVA代碼中通過動態產生的,然後再addView()加入到你自訂的View中,

private class SpeechView extends LinearLayout {          private TextView mTitle;          private TextView mDialogue;          public SpeechView(Context context, String title, String words) {              super(context);              this.setOrientation(VERTICAL);               // Here we build the child views in code. They could also have              // been specified in an XML file.            mTitle = new TextView(context);            mTitle.setText(title);              addView(mTitle, new LinearLayout.LayoutParams(                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));             mDialogue = new TextView(context);              mDialogue.setText(words);             addView(mDialogue, new LinearLayout.LayoutParams(                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));        }        /**         * Convenience method to set the title of a SpeechView         */                public void setTitle(String title) {                        mTitle.setText(title);         }                /**         * Convenience method to set the dialogue of a SpeechView         */         public void setDialogue(String words) {                mDialogue.setText(words);         }        } 

android自訂LinearLayout和View

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.