English 中文(简体)
Text Text
原标题:TextView setGravity() doesn t work in java

I m stuck on a problem and I don t know, what causes it. I have a very simple Layout like this:

<?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="wrap_content"
android:orientation="vertical">
<TextView
    android:id="@+id/my_text_view"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="NOTE_THIS"
    android:textColor="#FFFFFF"
    android:textSize="22dp"
    android:text="TestText"/>
</LinearLayout>

这包括在另一个布局内。 如果我改变Xml内部的重力,我就会看到拉伊特-埃迪特尔和我的电话中同样的结果。 如果我用<条码>米TextView.setGravity(重重度)等办法适用重心。 CENTER). It don t Change any. 我不能把瓦 Java的拉亚特瓦利定在案文中。

I ll tried for debug purposes to include them three times each with another gravity which does work even. So I assume everything is alright with my Layout and there has to be a Bug or something else I miss. Can someone give me a hint what I also can try, or what causes this problem?

最佳回答

您的文体为android:layout_width="liver_other",然后您可按方案方式使用myTextView.setGravity(重重重)。 CENTER

问题回答

你们需要确定LayoutParams物体的严重性,而不是观点本身:

TextView tv = new TextView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);

When you use LinearLayout as parent, then layout_gravity comes in picture which align control but not content inside the control so, Instead using android:layout_gravity use android:gravity.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签