很多时候,我们需要将TextView的字体自动调整到给定的边界.
可悲的是,即使有很多线程和帖子(和建议的解决方案)谈论这个问题(例如这里,这里和这里的例子),它们都没有真正运作良好.
这就是为什么,我决定测试每一个,直到找到真正的交易.
我认为来自这样一个textView的要求应该是:
应允许使用任何字体,字体,样式和字符集.
应该处理宽度和高度
没有截断,除非文本由于限制而无法适应,我们已经给它(例如:文本太长,可用大小太小).但是,如果我们愿意,我们可以请求水平/垂直滚动条,仅适用于那些情况.
应该允许多线或单线.如果是多行,请允许最大和最小行.
计算速度不应该慢.使用循环找到最佳尺寸?至少要优化它,不要每次增加1个采样.
在多行的情况下,应该允许更喜欢调整大小或使用更多行,和/或允许使用"\n"字符自己选择行.
我已经尝试了很多样本(包括链接的那些,我已经写过了),而且我也尝试修改它们来处理这些情况,我已经谈过了,但没有一个真正起作用.
我做了一个示例项目,让我可以直观地看到TextView是否正确自动匹配.
目前,我的示例项目只是随机化文本(英文字母加上数字)和textView的大小,并让它保持单行,但即使这样也不适用于我尝试的任何样本.
这是代码(也可在此处获得):
res/layout/activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:text="Button" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_above="@+id/button1"
android:layout_alignParentLeft="true" android:background="#ffff0000"
android:layout_alignParentRight="true" android:id="@+id/container"
android:layout_alignParentTop="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
src/.../MainActivity.javapublic class MainActivity extends Activity
{
private final Random _random =new Random();
private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); …Run Code Online (Sandbox Code Playgroud) 我有一个包含在相对布局中的线性布局.它在XML文件中设置为另一个线性布局的右侧(这很好).在某些情况下,我想在活动的onCreate期间更改布局的相对位置,因此我需要修改"右侧"参数以与另一个布局相关联.我试过这个:
RelativeLayout.LayoutParams layoutParams;
layoutParams = (RelativeLayout.LayoutParams) linearLayoutToMove
.getLayoutParams();
layoutParams.addRule(RelativeLayout.RIGHT_OF,
R.id.new_ref_LinearLayout);
Run Code Online (Sandbox Code Playgroud)
但它不起作用:o(
有线索吗?
我正在尝试编写自己的习惯View,但我遇到了问题LayoutParams.
想法是扩展ViewGroup(LinearLayout)
public class MyView extends LinearLayout{
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context) {
super(context);
}
public void putContent(){
setOrientation(HORIZONTAL);
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 5; i++){
View view = inflater.inflate(R.layout.item, null);
TextView tv = (TextView)view.findViewById(R.id.item_text);
tv.setText("Item " + i);
addView(view);
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,putContent方法会使项目膨胀并添加到我的视图中.这是一个项目布局
<?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:background="#FFFFFF">
<TextView android:text="TextView"
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我从不同的角度来看这个.基本上要点是:
我已经在XML中为一个以编程方式运行NEEDS的接口布局了一个模板,因此它将在运行期间动态填充.
这里的问题是,XML TextView有很多布局调整(有效),是必要的.但是如果我实际上在代码中设置它们,TextView甚至都不显示.
(顺便说一下,TextView嵌套在TableRow中,因此调用了权重.)我首先设计的XML模板,用作代码的参考是这样的,它工作得很好:
<TextView
android:id="@+id/txtviewWhiteBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/textview_9patch_white"
android:gravity="center"
android:text="75"
android:textColor="@android:color/black"
android:layout_margin="20dp"
android:padding="20dp"
android:textSize="40dp" />
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,完美地展示出来.当我在代码中运行相同的布局,并应用LayoutParams时,TextView消失.
这是相关的片段:
int textViewExampleID = 9001;
private TextView txtviewExample = new TextView(this);
private void buildTextView(){
LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
txtviewExample.setId(textViewExampleID);
txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
txtviewExample.setGravity(Gravity.CENTER);
txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
paramsExample.setMargins(20, 20, 20, 20);
txtviewExample.setPadding(20, 20, 20, 20);
txtviewExample.setTextSize(40);
txtviewExample.setText("customExample");
//if I comment out the following line, this TextView displays.
//if I leave it in, it doesn't display.
txtviewExample.setLayoutParams(paramsExample);
}
Run Code Online (Sandbox Code Playgroud)
我意识到LayoutParams有各种各样的可用类,我一直在玩它们所有的LinearLayout.LayoutParams,TableView.LayoutParams,RelativeLayout.LayoutParams,LayoutParams本身...无论我尝试哪一个,任何尝试调用"setLayoutParams"渲染整个TextView.我在这里搜索了论坛,但还没有找到答案.这不是那么罕见.
我有一个视图(包含广告),我需要缩放以适应某个时间点(加载广告后)的屏幕宽度.所以我有一个方法setHeight(myView)来计算给定屏幕宽度的正确高度,并相应地更改视图的LayoutParams.代码的关键部分是:
LayoutParams params = myView.getLayoutParams();
int width = myView.getWidth();
if (params != null && width > 0) {
params.height = (int) Math.round(ratio * width);
}
Run Code Online (Sandbox Code Playgroud)
这似乎在很大程度上起作用,但有时视图不会缩放.如果我在底部添加以下行,似乎只是合理地工作:
myView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
这似乎也有意义,因为Android Views会调用requestLayout()他们的setLayoutParams()方法.相反,我认为公共字段(params.height)中的更改不会触发布局更改.
另一方面,我反复在网上找到简单更改参数的教程,然后再不再设置视图.
所以,我的问题是:在更改LayoutParams的属性后立即更新布局是否正确,我需要setLayoutParams再次调用?只是更改属性只会导致布局更改在稍后某个时间从其他地方触发布局更改时?
有没有人知道如何android:layout_span=""在运行时设置/更改Android中的EditText.我已经在我的XML文件中定义了EditText.
<TableRow android:paddingTop="20dip" android:gravity="center_horizontal">
<EditText android:layout_width="150dip"
android:text=""
android:layout_height="40dip"
android:id="@+id/p"
android:layout_span="2">
</EditText>
</TableRow>
Run Code Online (Sandbox Code Playgroud) p我正在使用线性布局,并希望设置视图的最大高度.在"正常"情况下,我希望视图使用"wrap_content".然而,偶尔情况可能会将布局推向不合需要的尺寸.发生这种情况时,我想将高度限制在最大300dp.
当布局中的列表超过4个列表项时,我使用以下设置了视图的大小:
LinearLayout listLayout = (LinearLayout) dialog.findViewById(R.id.listLayout);
if(list.size() > 4){
LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);
listLayout.setLayoutParams(params);
}
Run Code Online (Sandbox Code Playgroud)
查看文档使我不了解所应用的度量单位.在这种情况下(dp,sp,px,...),有哪些计量单位?
运行测试,甚至将值设置为100都会使列表超出所需的高度.
请指教
我有一个EditText,一个Button和一个TextView.单击该按钮时,textview显示以edittext编写的文本.是否可以根据文本找到占用的textview的大小.即如果它有三个字符" abc ",现在是什么宽度,如果它有5个字符,如" abcde ",那么宽度是多少?
我已经设置了我的ImageView,如下所示:
<ImageView android:id="@+id/dl_image"
android:layout_width="60dp"
android:background="@drawable/pictureframe"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
Run Code Online (Sandbox Code Playgroud)
请注意,layout_width固定为60dp.根据我在线获取的内容,我想将此宽度调整为90dp或120dp(同时保持图像的宽高比).
我尝试使用setLayoutParams,但传递LayoutParams(120,LayoutParams.WRAP_CONTENT)会引发异常.它似乎不喜欢它.
如果可能的话,我试图避免为更大的尺寸制作另一个ImageView.
在我的应用程序中有一个LinearLayout,其布局高度为0.当我单击按钮时,此布局高度应为LayoutParams.WRAP_CONTENT.这是我在onclicklistner中使用的代码.
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
slider.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
我想动画这个.如何将动画设置为滑块.
android ×10
layoutparams ×10
textview ×3
font-size ×1
imageview ×1
layout ×1
tablelayout ×1
tablerow ×1