Android動態佈局,並動態為TextView控制元件設定drawableLeft、drawableRight等屬性新增圖示
注:(圖中每一個條目和圖示都是由程式碼動態生成)
程式碼動態佈局,並需要為每一個條目設定圖示,此時用到了 android:drawableLeft="@drawable/icon"
父xml檔案:
[html] view plaincopyprint?- <?xmlversion="1.0"encoding="utf-8"?>
- <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
-
android:layout_height
- android:background="@color/background">
- <!-- 子佈局由程式碼動態生成 -->
- <LinearLayout
- android:id="@+id/layout_CONTENT"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="@dimen/content_padding">
-
<
- android:id="@+id/activity_service_select"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/table_margin"
- android:background="@color/table_background"
-
android:orientation
- android:padding="@dimen/table_padding">
- </LinearLayout>
- </LinearLayout>
- </ScrollView>
子xml檔案: [html] view plaincopyprint?
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="@dimen/row_height"
- android:layout_marginBottom="@dimen/row_margin"
- android:background="@drawable/row_selector"
- android:paddingLeft="@dimen/row_padding_left"
- android:paddingRight="@dimen/row_padding_right">
- <TextView
- android:id="@+id/tv_select_item"
- style="@style/text_18"
- android:layout_width="match_parent"
- android:layout_height="@dimen/row_height"
- android:layout_marginBottom="@dimen/row_margin"
- android:background="@drawable/row_selector"
- android:gravity="center_vertical"
- android:textColor="@drawable/row_text_selector"/>
- <ImageView
- android:id="@+id/iv_icon"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_alignParentRight="true"
- android:duplicateParentState="true"
- android:gravity="center_vertical"
- android:src="@drawable/go"/>
- </RelativeLayout>
程式碼中引用: [java] view plaincopyprint?
- private ViewGroup mLayout;
- privateint img[] = {R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};
- /* (non-Javadoc)
- * @see app.ui.TitleActivity#onCreate(android.os.Bundle)
- */
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setUpViews();
- }
- privatevoid setUpViews()
- {
- setContentView(R.layout.activity_service_select);
- setTitle(R.string.text_select);
- showBackwardView(R.string.button_backward, true);
- mLayout = (ViewGroup)findViewById(R.id.activity_service_select);
- final String [] mSelfSelect = getResources().getStringArray(R.array.text_self_select);
- // 需要佈局的行數
- finalint rowCount = mSelfSelect.length;
- for (int i = 0; i < rowCount; i++) {
- final LinearLayout linearLayout = new LinearLayout(this);
- View.inflate(this, R.layout.service_examvaluable_item, linearLayout);
- final View view = linearLayout.getChildAt(0);
- view.setTag(i+1);
- view.setOnClickListener(this);
- Drawable drawable= getResources().getDrawable(img[i]);
- drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
- final TextView mTextView = (TextView) linearLayout.findViewById(R.id.tv_select_item);
- mTextView.setCompoundDrawables(drawable,null,null,null);//設定TextView的drawableleft
- mTextView.setCompoundDrawablePadding(10);//設定圖片和text之間的間距
- mTextView.setText(mSelfSelect[i]);
- // 新增到屏幕布局
- LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
- mLayout.addView(linearLayout, layoutParams);
- }
- }
在程式中直接取出子xml中TextView中的id,並動態設定改變了 DrawableLeft。
解決方案:
- publicvoid setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
類似呼叫方法如下:
1.在XML中使用
- android:drawableLeft="@drawable/icon"
2.程式碼中動態變化
- Drawable drawable= getResources().getDrawable(R.drawable.drawable);
- drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
- myTextview.setCompoundDrawables(drawable,null,null,null);
參考另一個函式:
- publicvoid setCompoundDrawablesWithIntrinsicBounds (Drawable left,
- Drawable top, Drawable right, Drawable bottom)
相關推薦
Android動態佈局,並動態為TextView控制元件設定drawableLeft、drawableRight等屬性新增圖示
注:(圖中每一個條目和圖示都是由程式碼動態生成) 程式碼動態佈局,並需要為每一個條目設定圖示,此時用到了 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
編寫一個函式escape(s,t),將字串t複製到字串s中,並在複製過程中將換行符、製表符等不可見字元分別轉換為\n、\t等相應的可見的轉義字元序列。要求使用switch語句。
#include <stdio.h> #include <ctype.h> #include <malloc.h> char *unescape(char s[], char t[]); char *escape(char s[], ch
練習 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中設定drawableLeft、drawableRight時設定圖片的大小方法
原文地址:https://blog.csdn.net/aiguoguo000/article/details/72842223 Drawable drawable = context.getDrawable(R.drawable.***); //(API
設定Textview的Drawableleft、Drawableright 等圖片與文字間距、位置和大小的方法
給自己記錄點筆記順便也給用的著的朋友給點參考設定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,完成後點關閉