0

私のアクティビティ レイアウトには、タイトル、写真、記事の 3 つの主要部分があります。タイトルを画面の 15%、画像を画面の 35%、記事のテキストを 50% にしようとしています。何らかの理由で、記事がより多くのスペースを占めることもあれば、写真が占めることもあります。寸法を強制する方法はありますか?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/headline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Headline" />

<ImageView
    android:id="@+id/picture"
    android:layout_width="fill_parent"
    android:layout_height ="wrap_content"
    android:layout_weight = "35"/>
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight = "50">
    <TextView
    android:id="@+id/storydata"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Story Data" />
</ScrollView>

4

1 に答える 1

2

私はあなたとほぼ同じ状況にあり、最終的にリソースファイルからxmlレイアウトを使用しようとすることをあきらめました。xmlをいじって、パーセンテージを正しく整列させようとして何時間費やしたかはわかりませんが、ある程度の成功はありましたが、本当に望んでいたことではありませんでした。

それで、私は最終的に、メインのアクティビティ内で、プログラムによって、自分でレイアウトを作成することにしました。それはかなりうまくいきました...

私のレイアウトは3つの部分です。上部に会社のロゴ/画像があり、画面の20%を占めたいと思っています。

中央のセクションには、ファイル名が入ったスクロールエリアがあります。ファイルリストは、上下左右にスクロールできます。この領域を画面の75%にします。

3番目のセクションは、中央のセクションのファイルリストを更新するために非同期呼び出しを行う単一の送信ボタンであり、画面の5%である必要があります。

だから、ここにコードがあります...

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Resources res = this.getResources();  //  Load the resources

    //  Get available screen size
    Display display = getWindowManager().getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setId(topLayout);
    layout.setBackgroundColor(0xff000000);
    LinearLayout.LayoutParams lp = new         LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    layout.setLayoutParams(lp);

    double logoHeight = screenHeight * .20;
    logoHeight = Math.round(logoHeight);

    Bitmap logoImg =  BitmapFactory.decodeResource(res, R.drawable.standardlogo);
    logoImg = Bitmap.createScaledBitmap(logoImg, screenWidth, (int)logoHeight, true);

    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(logoImg);
    imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,         LayoutParams.WRAP_CONTENT));
    layout.addView(imageView);

    double bottomHeight = screenHeight * .05;
    bottomHeight = Math.round(bottomHeight);

    int scrollAreaHeight = screenHeight - (int)logoHeight - (int)bottomHeight - topHeight;

    ScrollView scroll = new ScrollView(this);
    scroll.setBackgroundColor(0xffd8d8d8);
    LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(screenWidth,         scrollAreaHeight);
    scroll.setLayoutParams(slp);
    layout.addView(scroll);

    HorizontalScrollView hScroll = new HorizontalScrollView(this);
    hScroll.setBackgroundColor(0xffd8d8d8);
    LinearLayout.LayoutParams hlp = new LinearLayout.LayoutParams        (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    hScroll.setLayoutParams(hlp);
    scroll.addView(hScroll);

    TextView tv = new TextView(this);
    tv.setId(textArea);
    LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,         LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(lparams);
    tv.setTypeface(Typeface.MONOSPACE);         
    tv.setText("");
    hScroll.addView(tv);

    Button btn = new Button(this);
    btn.setId(sendButton);
    btn.setOnClickListener(sendBtnListener);
    ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(screenWidth,         LayoutParams.WRAP_CONTENT);
    btn.setLayoutParams(blp);
    btn.setText("List Import Directory");
    layout.addView(btn);

    setContentView(layout);    
}

これはかなりのコードですが、私が望んでいることを正確に実行し、テストしたすべての画面サイズで機能します。このアプリの会社のロゴpng画像は1つだけです。これは、10インチタブレット用の大きな画像です。

注:topHeightの値は100にハードコードされています。これは、テストしたすべてのデバイスで機能します。画面上部のアクションバーまたはステータスバーの高さであり、デバイスによって異なるため、最大2つの48ピクセルバーを処理するために100に設定しました。1つしかない場合は、画面下部に未使用のスペースが残ります。バーですが、最小限です。

于 2012-10-16T01:04:57.043 に答える