簡體   English   中英

Android:如何在EditText字段中更改文本大小?

[英]Android: How to change the textsize in an EditText field?

我想顯示一個表格,其中一些字段是可編輯的 - 請參閱此示例:

<?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="fill_parent"
 android:orientation="vertical"
 android:paddingLeft="1dip"
 android:paddingRight="1dip"
 android:paddingBottom="1dip"
 android:background="#F00"
 >
 <ScrollView
  android:id="@+id/ScrollView01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scrollbars="vertical"
  >
  <HorizontalScrollView
   android:id="@+id/HorizontalScrollView01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:scrollbars="horizontal"
   >
   <TableLayout
    android:id="@+id/ItemPane"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#EEE"
   >
    <TableRow
     android:id="@+id/DataRow"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"    
     android:background="#888"
     android:layout_margin="0dp" 
     android:gravity="left"
     >
     <TextView
      android:id="@+id/NameField"
      android:text="Fieldname"
      android:background="#EEE"
      android:textSize="14dp"
      android:layout_margin="1dp" 
      android:layout_marginBottom="0dp" 
      android:paddingLeft="3dp"
      android:paddingRight="3dp"
     />
     <EditText
      android:id="@+id/DataField"
      android:text="some field"
      android:background="#EEE"
      android:textSize="14dp"
      android:layout_margin="1dp" 
      android:layout_marginBottom="0dp" 
      android:paddingLeft="3dp"
      android:paddingRight="3dp"
      android:singleLine="true"
     />
     <EditText
      android:id="@+id/DataField2"
      android:text="some longish field content..."
      android:background="#EEE"
      android:textSize="14dp"
      android:layout_margin="1dp" 
      android:layout_marginBottom="0dp" 
      android:paddingLeft="3dp"
      android:paddingRight="3dp"
      android:singleLine="true"
     />
    </TableRow>   
   </TableLayout>
  </HorizontalScrollView>
 </ScrollView>
</LinearLayout>

為什么在EditText-fields中忽略了textSize屬性? 如何使這些字段中的文本變小,以便所有單元格具有相同的字體大小? 為什么Android不尊重該屬性,即使明確設置?

邁克爾

我認為你應該使用sp而不是dp。 機器人:TEXTSIZE = “20SP”

對textSize屬性嘗試'dip'而不是'dp'。 我不知道為什么,但有時使用'dp'沒有任何效果,即使文檔說這兩者是相同的。 所以我總是使用'​​dip'。

在main_Activity.java中嘗試以下操作

final Button btpls=(Button) findViewById(R.id.btpls);

btpls.setOnClickListener(new OnClickListener(){
    public void onClick(View t){
        float i=textfield.getTextSize();
       textfield.setTextSize(i+1) ; 
    }
});

final Button btmins=(Button) findViewById(R.id.btmins);

btmins.setOnClickListener(new OnClickListener(){
    public void onClick(View t){
        float n=textfield.getTextSize();
        textfield.setTextSize(n-1); 
    }
});

愚蠢的系統不會讓我貶低或評論。

sp也是密度無關的。 sp和dp之間的區別在於sp是根據用戶的字體大小首選項縮放的。 這意味着需要使用字體大小進行縮放的所有字體和用戶界面元素都應以sp為單位完成。 只有不應使用字體大小縮放的項目才能以dp為單位進行。

在styles.xml中

<style name="HintTextSize" parent="TextAppearance.Design.Hint">
        <item name="android:textSize">16sp</item>
        <item name="android:paddingLeft">16sp</item>
    </style> 

<android.support.design.widget.TextInputLayout
    android:id="@+id/trailer_one_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    **app:hintTextAppearance="@style/TextLabel"
    android:layout_marginTop="10dp">

        <EditText
            android:id="@+id/edit_text_trailer_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="@string/trailer_one"
            android:inputType="text"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/DrivingDarkGrey"/>
    </android.support.design.widget.TextInputLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM