GVKun编程网logo

如何以编程方式在 Android TextView 中设置 maxLength?(android 设置textview内容)

10

如果您想了解如何以编程方式在AndroidTextView中设置maxLength?和android设置textview内容的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何以编程方式在Andr

如果您想了解如何以编程方式在 Android TextView 中设置 maxLength?android 设置textview内容的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何以编程方式在 Android TextView 中设置 maxLength?的各个方面,并为您解答android 设置textview内容的疑在这篇文章中,我们将为您介绍如何以编程方式在 Android TextView 中设置 maxLength?的相关知识,同时也会详细的解释android 设置textview内容的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

如何以编程方式在 Android TextView 中设置 maxLength?(android 设置textview内容)

如何以编程方式在 Android TextView 中设置 maxLength?(android 设置textview内容)

我想以编程方式设置maxLength属性,TextView因为我不想在布局中对其进行硬编码。我看不到任何setmaxLength.

谁能指导我如何实现这一目标?

答案1

小编典典

应该是这样的。但从未将其用于 textview,仅用于 edittext :

TextView tv = new TextView(this);int maxLength = 10;InputFilter[] fArray = new InputFilter[1];fArray[0] = new InputFilter.LengthFilter(maxLength);tv.setFilters(fArray);

Android - 以编程方式设置 TextView TextStyle?

Android - 以编程方式设置 TextView TextStyle?

有没有办法以编程方式设置textStyle属性TextView?好像没有setTextStyle()方法。

需要明确的是,我不是在谈论 View / Widget 样式!我说的是以下内容:

<TextView  android:id="@+id/my_text"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="Hello World"  android:text/>

答案1

小编典典
textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface 是属性 textStyle。

正如 Shankar V 添加的那样,要保留以前设置的字体属性,您可以使用:

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);

Android TextView以编程方式跨越全宽

Android TextView以编程方式跨越全宽

我已经以编程方式在LinearLayout中创建了一个TextView:
LinearLayout filmTitleContainer = new LinearLayout(ctx);
TextView filmName = new TextView(ctx);
filmName.setBackgroundColor(Color.rgb(Color.YELLOW));
filmName.setText(this.screeningTime+" "+this.name);

文本视图缩小:

如何将文本设置为占据屏幕的整个宽度?当我使用XML布局文件时,我设置了android:layout_width =“match_parent”.什么是等效的Java代码?

解决方法

对视图及其容器使用LayoutParams:
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1f);
filmTitleContainer.setLayoutParams(params);
filmName.setLayoutParams(params);

android – Textview选框以编程方式

android – Textview选框以编程方式

尝试从数组中填充textview.我通过下面的代码设法通过 XML获得了预期的效果

<?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:orientation="vertical">

    <TextView
        android:id="@+id/marque_scrolling_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="16dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
        android:textSize="24sp" />
</LinearLayout>

但我需要在数组中使用其中的几个 – 所以我认为以编程方式创建文本视图可能是答案,但是无法将它们添加到字幕中.这是我正在努力的代码.

String[] strings = {"Mark","James","Paul"};
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);


        for(String s : strings)
        {
             TextView newTextView = new TextView(this);
             newTextView.setText(s);
             newTextView.setSingleLine(true);
             newTextView.setEllipsize(TextUtils.TruncateAt.END);       
             newTextView.setHorizontallyScrolling(true);
             newTextView.setLines(1);
             newTextView.setMarqueeRepeatLimit(-1);
             newTextView.setSelected(true);  
             newTextView.setTextSize(24);
             layout.addView(newTextView);
        }

解决方法

试试这个.

final RelativeLayout relativeLayout = new RelativeLayout(this);
final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
textView = new TextView(this);
textView.setId(1);

RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeLayoutbotombar.setLayoutParams(relativlaybottombar);


        textView.setText("text text text text text,with a long ");
        textView.setEllipsize(TruncateAt.MARQUEE);
        textView.setSelected(true);
        textView.setSingleLine(true);


        relativeLayout.addView(relativeLayoutbotombar);
        relativeLayoutbotombar.addView(textView);
        setContentView(relativeLayout,relativlayparamter);

另外一个选项:

TextView textView = (TextView) this.findViewById(R.id.xxx);  
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("Text text text text");
textView.setSelected(true);
textView.setSingleLine(true);

android – 以编程方式在cardview中设置边距

android – 以编程方式在cardview中设置边距

这是代码

CardView.LayoutParams layoutParams = new CardView.LayoutParams(
                CardView.LayoutParams.WRAP_CONTENT, CardView.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(100,100,100,100);
        CardView cv = new CardView(this);
        cv.setLayoutParams(layoutParams);

无论如何,从xml开始工作 –

android:layout_marginLeft="100dp"

可能是什么原因?我错过了什么!

我需要提升卡片的保证金

谢谢

解决方法:

如果我理解正确,您试图避免裁剪CardView的高程阴影.

而不是操纵边距,尝试将cardUseCompatPadding设置为true,如果要动态更改高程,则应在初始化CardView时调用setMaxCardElevation(float). (你可以通过xml和编程方式设置).

设置CardView的最大高程会让它占用更大阴影的空间,但它也会为较低的高度添加此空间.

顺便说一下,如果要为某些视图设置LayoutParams,则需要使用这些视图,这些视图对应于视图的父视图.

示例:LinearLayout中有一个TextView.

TextView textView = (TextView) findViewById(R.id.text_view);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

textView.setLayoutParams(layoutParams);

关于如何以编程方式在 Android TextView 中设置 maxLength?android 设置textview内容的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Android - 以编程方式设置 TextView TextStyle?、Android TextView以编程方式跨越全宽、android – Textview选框以编程方式、android – 以编程方式在cardview中设置边距的相关信息,请在本站寻找。

本文标签: