0

垂直方向と水平方向の中央に異なる数の行が存在する可能性があるn個のテキストビューを整列させようとしています。サイズと中央の配置が固定されている場合でも、より多くの行を持つテキストビューは「落下」します。それらをコードで整列させることは可能ですか?

これは私のxmlです:

<LinearLayout 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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="one line"
        android:textSize="48dp"
        tools:context=".MainActivity" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two lines"
        android:textSize="48dp" />

</LinearLayout>

これは私のJavaクラスです:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView oneline=(TextView) findViewById(R.id.textView1);
        TextView twolines=(TextView) findViewById(R.id.textView2);
        twolines.setText("two lines \n two lines");
        oneline.setHeight(200);
        twolines.setHeight(200);
        oneline.setWidth(400);
        twolines.setWidth(400);
        oneline.setBackgroundColor(Color.YELLOW);
        twolines.setBackgroundColor(Color.YELLOW);
        oneline.setGravity(Gravity.CENTER);
        twolines.setGravity(Gravity.CENTER);     
    }

そしてこれが結果です:-\ 斜め

4

1 に答える 1

2

なぜそれが起こるのかはわかりませんが、レイアウトを相対に変更すると問題は解決します。

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="one line"
        android:textSize="20sp"
        tools:context=".MainActivity" />


    <TextView
        android:layout_toRightOf="@id/textView1"
        android:id="@+id/textView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="top"
        android:background="@color/abs__holo_blue_light"
        android:gravity="center"
        android:text="two lines
two lines"
android:layout_gravity="center"
        android:textSize="20sp"
         />

</RelativeLayout>

編集 実際には、それは単なるbaseline_alignmentの問題でした。これがコードベースのソリューションです。

        TextView oneline=(TextView) findViewById(R.id.textView1);
        TextView twolines=(TextView) findViewById(R.id.textView2);
        twolines.setText("two lines \n ywo lines");
        oneline.setHeight(200);
        twolines.setHeight(200);
        oneline.setWidth(400);
        twolines.setWidth(400);
        oneline.setBackgroundColor(Color.YELLOW);
        twolines.setBackgroundColor(Color.RED);


        oneline.setGravity(Gravity.CENTER);
        twolines.setGravity(Gravity.CENTER);

        ViewParent v = ((View) oneline).getParent();
        ((LinearLayout) v).setBaselineAligned(false);
于 2013-03-07T15:39:47.060 に答える