1. 程式人生 > >Android動態佈局,並動態為TextView控制元件設定drawableLeft、drawableRight等屬性新增圖示

Android動態佈局,並動態為TextView控制元件設定drawableLeft、drawableRight等屬性新增圖示


注:(圖中每一個條目和圖示都是由程式碼動態生成)

程式碼動態佈局,並需要為每一個條目設定圖示,此時用到了 android:drawableLeft="@drawable/icon"

父xml檔案:

[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height
    ="match_parent"
  5.     android:background="@color/background">
  6.     <!-- 子佈局由程式碼動態生成 -->
  7.     <LinearLayout
  8.         android:id="@+id/layout_CONTENT"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent"
  11.         android:padding="@dimen/content_padding">
  12.         <
    LinearLayout
  13.             android:id="@+id/activity_service_select"
  14.             android:layout_width="match_parent"
  15.             android:layout_height="wrap_content"
  16.             android:layout_margin="@dimen/table_margin"
  17.             android:background="@color/table_background"
  18.             android:orientation
    ="vertical"
  19.             android:padding="@dimen/table_padding">
  20.         </LinearLayout>
  21.     </LinearLayout>
  22. </ScrollView>

子xml檔案: [html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="@dimen/row_height"
  4.     android:layout_marginBottom="@dimen/row_margin"
  5.     android:background="@drawable/row_selector"
  6.     android:paddingLeft="@dimen/row_padding_left"
  7.     android:paddingRight="@dimen/row_padding_right">
  8.     <TextView
  9.         android:id="@+id/tv_select_item"
  10.         style="@style/text_18"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="@dimen/row_height"
  13.         android:layout_marginBottom="@dimen/row_margin"
  14.         android:background="@drawable/row_selector"
  15.         android:gravity="center_vertical"
  16.         android:textColor="@drawable/row_text_selector"/>
  17.     <ImageView
  18.         android:id="@+id/iv_icon"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="match_parent"
  21.         android:layout_alignParentRight="true"
  22.         android:duplicateParentState="true"
  23.         android:gravity="center_vertical"
  24.         android:src="@drawable/go"/>
  25. </RelativeLayout>

程式碼中引用: [java] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. private ViewGroup mLayout;  
  2.     privateint img[] = {R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};  
  3.     /* (non-Javadoc) 
  4.      * @see app.ui.TitleActivity#onCreate(android.os.Bundle) 
  5.      */
  6.     @Override
  7.     protectedvoid onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setUpViews();  
  10.     }  
  11.     privatevoid setUpViews()  
  12.     {  
  13.         setContentView(R.layout.activity_service_select);  
  14.         setTitle(R.string.text_select);  
  15.         showBackwardView(R.string.button_backward, true);  
  16.         mLayout = (ViewGroup)findViewById(R.id.activity_service_select);  
  17.         final String [] mSelfSelect = getResources().getStringArray(R.array.text_self_select);  
  18.         // 需要佈局的行數
  19.         finalint rowCount = mSelfSelect.length;  
  20.         for (int i = 0; i < rowCount; i++) {  
  21.             final LinearLayout linearLayout = new LinearLayout(this);  
  22.             View.inflate(this, R.layout.service_examvaluable_item, linearLayout);  
  23.             final View view = linearLayout.getChildAt(0);  
  24.             view.setTag(i+1);  
  25.             view.setOnClickListener(this);  
  26.             Drawable drawable= getResources().getDrawable(img[i]);  
  27.             drawable.setBounds(00, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
  28.             final TextView mTextView = (TextView) linearLayout.findViewById(R.id.tv_select_item);  
  29.             mTextView.setCompoundDrawables(drawable,null,null,null);//設定TextView的drawableleft
  30.             mTextView.setCompoundDrawablePadding(10);//設定圖片和text之間的間距
  31.             mTextView.setText(mSelfSelect[i]);  
  32.             // 新增到屏幕布局
  33.             LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);  
  34.             mLayout.addView(linearLayout, layoutParams);  
  35.         }  
  36.     }  


在程式中直接取出子xml中TextView中的id,並動態設定改變了 DrawableLeft。

解決方案:

  1. publicvoid  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);  


類似呼叫方法如下:

1.在XML中使用

  1. android:drawableLeft="@drawable/icon"


2.程式碼中動態變化

  1. Drawable drawable= getResources().getDrawable(R.drawable.drawable);  
  2. drawable.setBounds(00, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
  3. myTextview.setCompoundDrawables(drawable,null,null,null);  

參考另一個函式:

  1. publicvoid setCompoundDrawablesWithIntrinsicBounds (Drawable left,  
  2. Drawable top, Drawable right, Drawable bottom)  

相關推薦

Android動態佈局動態TextView控制元件設定drawableLeftdrawableRight屬性新增圖示

注:(圖中每一個條目和圖示都是由程式碼動態生成) 程式碼動態佈局,並需要為每一個條目設定圖示,此時用到了 android:drawableLeft="@drawable/icon" 父x

android開發時日期控制元件設定選擇範圍再次點選時顯示上次選擇日期

下面做的是:28天<= 日期 <=65週歲 //計算28天前的日期 public static final long _MAX_TIME = 28 * 24 * 60 * 60 * 1000L;int inComingYear;int inComingMonth;int

android studio的preview看不到佈局導致看不到控制元件的效果

android Studio新建專案,preview只顯示一個大白版,看不到任何控制元件,不能預覽。 這時候我上網查了很多資料,最後發現可能是這幾個原因導致的: 1。 Invalidate caches/Restart... 2.  force refresh st

android圖片點選放大動畫遮擋旁邊的控制元件

首先是點選放大 可以使用android自帶的縮放動畫,因為要遮蓋其他控制元件,就需要控制元件處在最上層,這裡需要呼叫bringTofront方法 @Overridepublic boolean onTouch(View v, MotionEvent event) {// T

練習 3-2 編寫一個函式escape(s, t)將字串t 複製到字串s 中在複製過程中將換行符製表符不可見字元分別轉換\n\t相應的可見的轉義字元序列。要求使用swich語句。

要將所有的轉義字元都進行轉換,分支會有很多,在這裡以換行符和製表符為例進行轉換。 #include<stdio.h> void escape(char s[],char t[]); vo

使用程式碼TextView或者Button設定drawableLeft,top,ringht,buttom

大家都熟悉怎麼在xml中為TextView或者Button設定方點陣圖,但是如何在程式碼中實現呢? 現提供在程式碼中設定方點陣圖的函式: private void setDrawableDir()

安卓下如何使用XmlPullParser解析xml檔案顯示在TextView控制元件

解析xml檔案有好多種方式,今天介紹下XmlPullParser怎麼解析xml檔案,既然是要解析xml檔案首先得需要一個xml檔案 如下weather.xml檔案 <?xml version="1.0" encoding="utf-8"?> <weathe

TextView設定drawableLeftdrawableRight設定圖片的大小方法

原文地址:https://blog.csdn.net/aiguoguo000/article/details/72842223   Drawable drawable = context.getDrawable(R.drawable.***);  //(API

設定TextviewDrawableleftDrawableright 圖片與文字間距位置和大小的方法

給自己記錄點筆記順便也給用的著的朋友給點參考設定Textview的Drawableleft 等圖片的間距的問題 以Textview舉例 想得到如下圖的Textview效果public class MyText extends TextView {    public MyT

Android學習筆記之Dialog自定義佈局說明空指標問題

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

div寬度固定將其中的文字換行動態獲取div的高

rip title oat clas 獲取 pre jquery logs -c <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title&

用js把圖片做的富有動態對以後需要用著的屬性進行封裝

進行 display div lin 圖片 doc 移除 win next 首先我們先要導入幾張圖片(我已導入完畢): ; 好,我們先寫一個 <div ></div>, 定義一個 class="contair", 在這<div>

element el-tree循環遍歷樹形結構動態賦值disabled屬性

lse form 遍歷樹 alt fin img all uid 2-2 淩晨3點,功夫不負有心人,已經累趴,效果終於出來: 貼上代碼: <style scoped> .form { width: 50%; } </style>

微信小程式 修改資料動態渲染頁面;修改陣列;

一、修改資料,並在頁面動態渲染   this.setData({     txt: '12112'   }) 二、修改陣列 var rotateClassItem = 'rotateClass['+ index + ']'; t

react-native-android-unity(二)建立unity專案匯出android程式碼包嵌入android專案中

1.建立unity專案 給Main Camera新增指令碼Android,使用C#開發,指令碼內容如下: using System.Collections;
using System.Colle

android上用C語言讀取fb0實現截圖儲存rgb565的bmp

好久沒有看,這兩天在折騰一下,更新一下:修正了framebuffer bgra_8888格式截圖變紅的問題 =================================       android上用C語言讀取fb0實現截圖,儲存為bmp圖片, 支援16位

Android FrameLayout佈局中的控制元件設定居中動態設定

Android FrameLayout 佈局檔案靜態設定裡面的控制元件時是預設左上角疊加的。 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_w

利用c#實現dll動態在c++中呼叫的方法

           近期,在進行一個大專案開發。其中涉及多語言協同開發。主要是c#dll和c++dll的開發和應用,其中,需要在c++中呼叫c#dll的內容。現在把開發中的經驗、教訓和注意事項總結整理如下,希望對其他人能有所幫助。           1.建立c#dll,

C#WinForm程式呼叫SVG動態在SVG圖上綁值

開始建立一個新的C #窗體應用程式,然後安裝SVG渲染庫使用NuGet包管理參考。 一、右鍵單擊解決方案資源管理器,選擇管理NuGet程式包; 二、選擇聯機,右上角搜尋框輸入SVG; 三、安裝SVG Rendering Library,完成後點關閉