欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android動(dòng)態(tài)添加設(shè)置布局與控件的方法

 更新時(shí)間:2016年01月13日 11:27:47   作者:chenguang79  
這篇文章主要介紹了Android動(dòng)態(tài)添加設(shè)置布局與控件的方法,涉及Android中布局與控件的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android動(dòng)態(tài)添加設(shè)置布局與控件的方法。分享給大家供大家參考,具體如下:

有時(shí)候我們會(huì)在代碼端,動(dòng)態(tài)的設(shè)置,添加布局和控件。下面我們就看來看一下如何處理,直接上代碼,代碼里面的注解很清楚了。

布局文件:fragment_hot.xml

說明:這個(gè)部局,我用的是scrollView做為基礎(chǔ)布局,主要是為了實(shí)現(xiàn)一個(gè)滾動(dòng)。這里不多說,這個(gè)你可以使用任何布局都可以,這里的id我是提前定義的。

這里面的現(xiàn)在有的布局是我為了看到我在代碼端,動(dòng)態(tài)添加的代碼,是否可以追加到現(xiàn)有布局的后面而加上,這里大家可以自己設(shè)置

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@id/id_stickynavlayout_innerscrollview"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
    android:id="@+id/line_fragment_hot_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="80dp"
      android:orientation="horizontal">
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1">
        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/desktop_device"/>
      </LinearLayout>
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊孔雀城"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊 燕郊"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="3萬抵6萬"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="6000元/m"/>
      </LinearLayout>
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:gravity="right">
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="剩2天"
          android:textColor="#F97F49"
          android:textSize="12dp"
          android:textStyle="bold"
          android:layout_marginTop="2dp"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/rectangle_radius_fen"
          android:gravity="center"
          android:text="團(tuán)"
          android:textSize="9dp"
          android:textColor="#ffffff"
          android:layout_marginRight="5dp"
          android:layout_marginLeft="2dp"/>
      </LinearLayout>
    </LinearLayout>
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="80dp"
      android:orientation="horizontal">
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1">
        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/desktop_device"/>
      </LinearLayout>
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊孔雀城"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="燕郊 燕郊"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="3萬抵6萬"/>
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="6000元/m"/>
      </LinearLayout>
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:gravity="right">
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="20dp"
          android:text="剩2天"/>
        <TextView
          android:layout_width="10dp"
          android:layout_height="10dp"
          android:background="@drawable/rectangle_radius_fen"
          android:gravity="center"
          android:text="團(tuán)"
          android:textSize="5dp"
          android:layout_marginRight="5dp"/>
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
</ScrollView>

一個(gè)背景圖文件在drawable文件夾中

rectangle_radius_fen.xml

說明:很簡(jiǎn)單,就是設(shè)置一個(gè)有圓角的方形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <!-- 定義填充漸變顏色 -->
  <solid android:color="#F97F49"/>
  <!-- 設(shè)置內(nèi)填充 -->
  <padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"/>
  <!-- 設(shè)置圓角矩形 -->
  <corners android:radius="2dp"/>
</shape>

后臺(tái)代碼:

fragment_hot.java

說明:這里不多說了,里面的注解很明確了

package com.example.cg.fangduo;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
/**
 * A simple {@link Fragment} subclass.
 */
public class fragment_hot extends Fragment {
  //定義當(dāng)前頁面
  private View view;
  private LinearLayout line_fragment_hot_main;
  private ScrollView id_stickynavlayout_innerscrollview;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_hot, container, false);
    id_stickynavlayout_innerscrollview = (ScrollView)view.findViewById(R.id.id_stickynavlayout_innerscrollview);
    line_fragment_hot_main = (LinearLayout)view.findViewById(R.id.line_fragment_hot_main);
    for(int i=0;i<10;i++) {
      /**
       * 設(shè)置每個(gè)item最外層的LinearLayout
       */
      LinearLayout itemMain = new LinearLayout(getActivity());
      itemMain.setOrientation(LinearLayout.HORIZONTAL); //設(shè)置linearLayout是橫向還是豎各
      LinearLayout.LayoutParams itemMainparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(getActivity(), 80)); //設(shè)置寬與高
      itemMainparams.setMargins(0,dip2px(getActivity(),1),0,0); //設(shè)置每個(gè)item與上一個(gè)控件的間隔是1dip
      itemMain.setLayoutParams(itemMainparams);
      itemMain.setBackgroundColor(Color.WHITE); //設(shè)置背景色
      itemMain.setVerticalGravity(Gravity.CENTER); //設(shè)置對(duì)齊方式
      /**
       * 設(shè)置左側(cè)圖片LinearLayout
       */
      LinearLayout itempic = new LinearLayout(getActivity());
      itempic.setOrientation(LinearLayout.HORIZONTAL);
      LinearLayout.LayoutParams itempicparams = new LinearLayout.LayoutParams(0, dip2px(getActivity(), 70), 1);
      itempic.setLayoutParams(itempicparams);
      itempic.setVerticalGravity(Gravity.CENTER_VERTICAL);
      /**
       * 設(shè)置圖片
       */
      ImageView imgPic = new ImageView(getActivity());
      imgPic.setImageResource(R.drawable.desktop_device);
      LinearLayout.LayoutParams imgPicparams = new LinearLayout.LayoutParams(dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT), dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT));
      imgPicparams.leftMargin = dip2px(getActivity(), 5);
      imgPicparams.gravity = Gravity.LEFT;  //必須要加上這句,setMargins才會(huì)起作用,而且此句還必須在setMargins下面
      imgPic.setLayoutParams(imgPicparams);
      /**
       * 圖片LinearLayout加載圖片
       */
      itempic.addView(imgPic, imgPicparams);
      /**
       * 向主LinearLayout加載圖片LinearLayout
       */
      itemMain.addView(itempic);
      /**
       * 設(shè)置中間文字顯示LinearLayout
       */
      LinearLayout itemtext = new LinearLayout(getActivity());
      itemtext.setOrientation(LinearLayout.VERTICAL);
      LinearLayout.LayoutParams itemtextparams = new LinearLayout.LayoutParams(0, dip2px(getActivity(), 70), 1);
      itemtext.setLayoutParams(itemtextparams);
      itemtext.setVerticalGravity(Gravity.CENTER_VERTICAL);
      TextView txtOne = new TextView(getActivity());
      txtOne.setText("珠江俊景小區(qū)");
      txtOne.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12); //設(shè)置字號(hào),并且設(shè)置它的單位為dip
      txtOne.getPaint().setFakeBoldText(true); //字體加租
      LinearLayout.LayoutParams txtOneparams = new LinearLayout.LayoutParams(dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT), dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT));
      txtOneparams.weight = 1;
      txtOneparams.setMargins(0, 0, 0, 0);
      txtOneparams.gravity = Gravity.LEFT;
      txtOne.setLayoutParams(txtOneparams);
      itemtext.addView(txtOne);
      TextView txtTwo = new TextView(getActivity());
      txtTwo.setText("哈爾濱");
      txtTwo.getPaint().setFakeBoldText(true);
      txtTwo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8);
      LinearLayout.LayoutParams txtTwoparams = new LinearLayout.LayoutParams(dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT), dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT));
      txtTwoparams.weight = 1;
      txtTwo.setLayoutParams(txtTwoparams);
      itemtext.addView(txtTwo);
      TextView txtThree = new TextView(getActivity());
      txtThree.setText("一萬抵五萬");
      txtThree.getPaint().setFakeBoldText(true);
      txtThree.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8);
      txtThree.setLayoutParams(txtOneparams);
      itemtext.addView(txtThree);
      TextView txtFour = new TextView(getActivity());
      txtFour.setText("8000元/m");
      txtFour.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
      txtFour.setTextColor(Color.parseColor("#F97F49"));
      txtFour.getPaint().setFakeBoldText(true);
      txtFour.setLayoutParams(txtOneparams);
      itemtext.addView(txtFour);
      itemMain.addView(itemtext);
      /**
       * 定義右側(cè)顯示信息框架
       */
      LinearLayout line_right_text = new LinearLayout(getActivity());
      line_right_text.setOrientation(LinearLayout.HORIZONTAL);
      LinearLayout.LayoutParams rightparams = new LinearLayout.LayoutParams(0, dip2px(getActivity(), 70), 1);
      line_right_text.setLayoutParams(rightparams);
      line_right_text.setGravity(Gravity.RIGHT);  //右對(duì)齊
      TextView daytxt = new TextView(getActivity());
      LinearLayout.LayoutParams daytxtparams = new LinearLayout.LayoutParams(dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT), dip2px(getActivity(), ViewGroup.LayoutParams.WRAP_CONTENT));
      daytxt.setLayoutParams(daytxtparams);
      daytxt.setText("剩2天");
      daytxt.setTextColor(Color.parseColor("#F97F49")); //設(shè)置顏色
      daytxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); //設(shè)置文字大小
      daytxt.getPaint().setFakeBoldText(true); //設(shè)置文字樣式,加粗
      //daytxt.setPadding(0,2,2,0);
      line_right_text.addView(daytxt);
      TextView tuantxt = new TextView(getActivity());
      LinearLayout.LayoutParams tuantxtparams = new LinearLayout.LayoutParams(dip2px(getActivity(), 12), dip2px(getActivity(), 12));
      tuantxtparams.setMargins(dip2px(getActivity(), 2), dip2px(getActivity(), 0), dip2px(getActivity(), 5), 0);
      tuantxt.setLayoutParams(tuantxtparams);
      tuantxt.setBackgroundResource(R.drawable.rectangle_radius_fen);  //設(shè)置textView背景圖片
      tuantxt.setGravity(Gravity.CENTER);
      tuantxt.setText("團(tuán)");
      txtFour.getPaint().setFakeBoldText(true);
      tuantxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8);
      tuantxt.setTextColor(Color.parseColor("#ffffff"));
      line_right_text.addView(tuantxt);
      itemMain.addView(line_right_text);
      line_fragment_hot_main.addView(itemMain);
    }
    return view;
  }
  /**
   * 設(shè)備像素(dip,dp)轉(zhuǎn)屏幕像素(px)
   * px就是像素,如果用px,就會(huì)用實(shí)際像素畫,比個(gè)如吧,用畫一條長(zhǎng)度為240px的橫線,在480寬的模擬器上看就是一半的屏寬,而在320寬的模擬器上看就是2/3的屏寬了。
       * 而dip,就是把屏幕的高分成480分,寬分成320分。比如你做一條160dip的橫線,無論你在320還480的模擬器上,都是一半屏的長(zhǎng)度。
   * @param context
   * @param dipValue
   * @return
   */
  public static int dip2px(Context context, float dipValue){
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int)(dipValue * scale + 0.5f);
  }
  /**
   * 將px值轉(zhuǎn)換為sp值,保證文字大小不變
   *
   * @param pxValue
   * @param pxValue
   *      (DisplayMetrics類中屬性scaledDensity)
   * @return
   */
  public static int px2sp(Context context, float pxValue) {
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue / fontScale + 0.5f);
  }
  /**
   * 將sp值轉(zhuǎn)換為px值,保證文字大小不變
   *
   * @param spValue
   * @param spValue
   *      (DisplayMetrics類中屬性scaledDensity)
   * @return
   */
  public static int sp2px(Context context, float spValue) {
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * fontScale + 0.5f);
  }
}

效果圖:

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • android選項(xiàng)卡TabHost功能用法詳解

    android選項(xiàng)卡TabHost功能用法詳解

    這篇文章主要為大家詳細(xì)介紹了android選項(xiàng)卡TabHost的功能用法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android LeakCanary的使用方法介紹

    Android LeakCanary的使用方法介紹

    在Android的性能優(yōu)化中,內(nèi)存優(yōu)化是必不可少的點(diǎn),而內(nèi)存優(yōu)化最重要的一點(diǎn)就是解決內(nèi)存泄漏的問題,在Android的內(nèi)存泄漏分析工具也不少,比如PC端的有:AndroidStudio自帶的Android Profiler、MAT等工具;手機(jī)端也有,就是我們今天要介紹的LeakCanary
    2022-09-09
  • Android5.0中Material Design的新特性

    Android5.0中Material Design的新特性

    這篇文章主要介紹了Android5.0中Material Design的新特性的相關(guān)資料,需要的朋友可以參考下
    2016-08-08
  • Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)

    Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)

    這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android自定義View實(shí)現(xiàn)公交成軌跡圖

    Android自定義View實(shí)現(xiàn)公交成軌跡圖

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)公交成軌跡圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Android item長(zhǎng)按刪除功能

    Android item長(zhǎng)按刪除功能

    這篇文章主要介紹了Android item長(zhǎng)按刪除功能,在文章底部給大家介紹了android 長(zhǎng)按刪除listview的item的實(shí)例代碼,需要的的朋友參考下
    2017-07-07
  • RecyclerView 源碼淺析測(cè)量 布局 繪制 預(yù)布局

    RecyclerView 源碼淺析測(cè)量 布局 繪制 預(yù)布局

    這篇文章主要介紹了RecyclerView 源碼淺析測(cè)量 布局 繪制 預(yù)布局,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android NDK開發(fā)(C語言-文件讀寫)

    Android NDK開發(fā)(C語言-文件讀寫)

    這篇文章主要介紹了Android NDK開發(fā)C語言文件讀寫,一個(gè)文件,無論它是文本文件還是二進(jìn)制文件,都是代表了一系列的字節(jié)。下面我們就來看看在Android NDK開發(fā)中的C語言文件讀寫詳細(xì)內(nèi)容吧,需要的朋友可以參考一下
    2021-12-12
  • Android uses-permission權(quán)限列表中文注釋版

    Android uses-permission權(quán)限列表中文注釋版

    Android有一個(gè)精心設(shè)計(jì)的安全模型。每一個(gè)應(yīng)用都有其自己Linux用戶和群組,在單獨(dú)的進(jìn)程和VM上運(yùn)行,不能影響到其他應(yīng)用
    2014-05-05
  • Android自定義相機(jī)、預(yù)覽區(qū)域裁剪

    Android自定義相機(jī)、預(yù)覽區(qū)域裁剪

    這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)、預(yù)覽區(qū)域裁剪,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評(píng)論